Piping search term (not filename) to grep

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x