0

I am trying to search the file dep/playlist for 'ohn'. Then, I would like to take this result and apply it to a new grep command that searches the file employee list end then echoes the results on screen. The code below isn't behaving as expected.

grep ohn dep/playlist > search
grep $(cat search) employeelist > newlist
cat newlist

Thanks,

Tim

2 Answers 2

4

You need to tell grep to obtain the patterns from a file, use the -f option:

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

So the command would look like:

grep -f search employeelist > newlist

Using process substitution you could obviate the need of a temporary file. So the two grep commands could be written into one as:

grep -f <(grep ohn dep/playlist) employeelist > newlist
Sign up to request clarification or add additional context in comments.

Comments

0

xargs:

grep ohn dep/playlist | xargs -I name grep name employeelist

This searches dep/playlist for 'ohn', and then upon finding a result, that result is then used in grep X employeelist, where X is the result from the first grep.

1 Comment

I would suggest adding -n 1 option to xargs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.