29

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?

1

1 Answer 1

43

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

Your Answer

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

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