frank
frank

Reputation: 1811

bash pipe and printing with multiple filter

I was wondering if something like this exist:

tail -f file1 | grep "hello" > fileHello | grep "bye" > fileBye | grep "etc" > fileEtc
echo b1bla >> file1  
echo b2hello >> file1
echo b3bye >> file1
echo b4hellobye >> file1
echo b5etc >> file1
echo b6byeetc >> file1

That will make that result :

file1:

b1bla
b2hello
b3bye
b4hellobye
b5etc
b6byeetc

fileHello:

b2hello
b4hellobye

fileBye:

b3bye
b4hellobye
b6byeetc

fileEtc:

b5etc
b6byeetc

Thanks!

Upvotes: 1

Views: 120

Answers (2)

konsolebox
konsolebox

Reputation: 75588

Use tee with process substitution:

tail -f file1 | tee >(exec grep "hello" > fileHello) >(exec grep "bye" > fileBye) | grep "etc" > fileEtc

Upvotes: 2

William Pursell
William Pursell

Reputation: 212584

This works, but be aware that piping tail -f is likely to cause some unexpected buffering issues.

tail -f file1 |
 awk '/hello/ { print > "fileHello"}
   /bye/ { print > "fileBye"}
   /etc/ { print > "fileEtc"}' 

Upvotes: 1

Related Questions