penguin_king
penguin_king

Reputation: 37

multiple grep wildcard sequentially

Example:

I have a script

grep  "foo" /home/user_a/*/*_test >> list
grep -o "bar" /home/user_b/*/*_word >> list
printf "\n" >> list

How do I run both grep at so that * is in the same iteration?

Assuming all the files under /home/user_a only has a string "foo", and "bar" for files under /home/user_b

How to get this:

foobar
foobar
foobar

Instead of:

foofoofoobarbarbar

We can think of the wildcard like a for loop, iterating all the matched files.

Instead of waiting the first grep to finish before running the second grep, the first grep stops at the first resutlt and goes to the second grep and get the first result, and then goes back to the first grep again, and so on..

Upvotes: 1

Views: 158

Answers (1)

John1024
John1024

Reputation: 113994

If you want to alternate between the output of two commands, grep commands in this case, then use paste, like this:

paste <(grep -h "foo" /home/user_a/*/*_test) <(grep -ho "bar" /home/user_b/*/*_word)

Since you didn't seem to want the filenames in the output, I added the -h option to both grep commands.

Simpler example

To see how paste combines the output of two different commands, try this:

$ paste <(echo a; echo b) <(echo 1; echo 2)
a       1
b       2

Upvotes: 1

Related Questions