Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

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
share|improve this answer
    
Thanks for the tip, but it just produced: grep: -: No such file or directory –  The Unfun Cat Aug 4 '13 at 6:20
    
@TheUnfunCat Try it without xargs –  Joseph R. Aug 4 '13 at 6:22
    
Didn't work unfortunately. Same error. –  The Unfun Cat Aug 4 '13 at 6:24
1  
If you're not using gnu grep, try -f /dev/stdin instead of -f - –  rici Aug 4 '13 at 6:30
3  
You can also use bash's <() syntax: grep -f <(awk ...) os_clusters/piRNA_clusters.bed –  rici Aug 4 '13 at 6:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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