Erunamo114
Erunamo114

Reputation: 151

What is the proper method to pipe the output of the cut command into a grep command?

I am currently learning a little more about using Bash shell on OSX terminal. I am trying to pipe the output of a cut command into a grep command, but the grep command is not giving any output even though I know there are matches. I am using the following command:

cut -d'|' -f2 <filename.txt> > <temp.txt> | grep -Ff <temp.txt> <searchfile.txt> > <filematches.txt>

I was thinking that this should work, but most of the examples I have seen normally pipe grep output into the cut. My goal was to cut field 2 from the file and use that as the pattern to search for in . However, using the command produced no output.

When I generated the temp.txt first with the cut command and then ran the grep on it manually with no pipe, the grep seemed to run fine. I am not sure why this is?

Upvotes: 2

Views: 3637

Answers (2)

Charlie Martin
Charlie Martin

Reputation: 112366

Okay, a reason this line doesn't behave as you expect

cut -d'|' -f2 <filename.txt> > <temp.txt> | grep -Ff <temp.txt> <searchfile.txt> > <filematches.txt>

is that the output of your cut is going to temp.txt. You're not sending anything to the pipe. Now, conveniently pipe also starts a new commend, so it doesn't matter much -- grep runs and reads searchfile.txt.

But what are you trying to do? Here's what your command line is trying to do:

  1. take the second pipe-delimited field from filename.txt
  2. write it to a file
  3. run grep ...
  4. ... using the contents of the file from 2 as a grep search string (which isn't going to do what you think either, as you're effectively asking grep to look for the pattern match1\nmatch2...)

You'd be closer with

cut ... && grep ...

as that runs grep assuming cut completes effectively. Or you could use

grep -f `cut ...` 

which would put the results on the command line. You need to mess with quoting, but you're still going to be looking for a line containing ALL of your match fields from cut.

I'd recommend maybe you mean something like this:

for match in `cut ...`
do
    grep -f $match >> filematches.txt
done

Upvotes: 0

anubhava
anubhava

Reputation: 785098

You can use process substitution here:

grep -Ff <(cut -d'|' -f2 filename.txt) searchfile.txt > filematches.txt

<(cut -d'|' -f2 filename.txt) is feeding cut command's output to grep as a file.

Upvotes: 5

Related Questions