I’ve just needed to do the following on the linux command line:
- Search through files within a directory for a certain bit of text
- 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.
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
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!

