I’ve just needed to do the following on the linux command line:

  1. Search through files within a directory for a certain bit of text
  2. Display the unique files with that bit of text in there

This required a bit of jiggery.  First I’ll show how you do it using three commands, then I’ll show you the shortcut method which I used which really displays the power of linux/unix command line workflows.

First we have to use grep on the files within a directory, which is done like this:

grep -rin keyword(s) directory > outputfile

The we have to get rid of the non-useful bits that grep outputs:

sed 's/:.*//g' outputfile> outputfile

Finally we have to get all of those unique filenames:

uniq outputfile > outputfile

It is quite neat that we can even do it as I’ve shown above. But here is the Pièce de résistance, the linux/unix piping:

grep -rin keyword . | sed 's/:.*//g' | uniq > outputfile.csv

Here we see I’ve piped the output of grep into sed and then into uniq, which outputs its result into a CSV file… so none of that working-on-and-saving-files-multiple-times malarkey. All your lines are consolidated into one easily manageable line :-)

Hopefully thats quite a neat example of piping, grepping, sedding and uniqing… feel free to use, and feel free to comment!