jipot
jipot

Reputation: 94

How to sed output of a command and not supress output

I have the example text stored in test.sh:

echo 'Hello world 123'
echo 'some other text' 

With the following command in a bash script:

word123=$(./test.sh |sed -nr 's/Hello world (.*)/\1/p' )

This works correctly and outputs:

123

However, this does output

Hello world 123
some other text

Is there a way to capture the text in 123 and also output everything else in the file?

Upvotes: 0

Views: 69

Answers (1)

Cyrus
Cyrus

Reputation: 88766

With Linux, bash and tee:

word123=$( ./test.sh | tee >&255 >(sed -nr 's/Hello world (.*)/\1/p') )

File descriptor 255 is a non-redirected copy of stdout.

See: What is the use of file descriptor 255 in bash process

Upvotes: 2

Related Questions