23

I want to pick out certain lines containing a given number from a file. The file I want to search is called os_clusters/piRNA_clusters.bed.

awk '{if (a[$0]++ == 0) {split($0,b,"."); ;split(b[1],c,"r"); print c[3]}};' test_non_enriched | xargs grep {} os_clusters/piRNA_clusters.bed

The first part, before the pipe, works- it produces the terms to search for, such as 8707, 8824 etc. However, the latter part does not.

awk '' ... | xargs grep {} os_clusters/piRNA_clusters.bed

Instead of searching the target file for the terms produced by the pipe, it considers the search terms as the input file. Hence, I get error messages like:

grep: 8707: No such file or directory
grep: 8824: No such file or directory

What do I need to change to search the file os_clusters/piRNA_clusters.bed for the terms produced by the pipe?

35

I think you want

... | grep -f - os_clusters/piRNA_clusters.bed

-f tells grep to obtain its search pattern from a file and - tells it that this file is actually stdin (the output of the pipe in your case).

Thanks to @rici's comment, for non-GNU grep use

... | grep -f /dev/stdin os_clusters/piRNA_clusters.bed
  • Thanks for the tip, but it just produced: grep: -: No such file or directory – The Unfun Cat Aug 4 '13 at 6:20
  • 7
    If you're not using gnu grep, try -f /dev/stdin instead of -f - – rici Aug 4 '13 at 6:30
  • OS X 10.8 aka Mountain Lion – The Unfun Cat Aug 4 '13 at 6:31
  • 6
    You can also use bash's <() syntax: grep -f <(awk ...) os_clusters/piRNA_clusters.bed – rici Aug 4 '13 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.