Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file containing words (one per line) such as


I have a secondary file in CSV format such as


I would like to use grep with the first file being the search pattern, if a line in the secondary file contains a word in the first file I would like to entirely remove the line in the secondary file.

I am not sure grep is even the way to go about this.

EDIT: bash scripting

share|improve this question
sed or awk is what you want, not grep. – Zoredache May 10 '13 at 18:43
How will you handle escaping issues in the csv? This could be a really complex set of regexs. – jpaugh May 10 '13 at 21:17
up vote 9 down vote accepted

You can do this using grep's -f option (that's lower-case -f, not -F):

% echo -e 'Dog\nFish\nCat\nShoes' > ./file1.txt 
% echo -e '1,shoes,red\n2,apple,black\n3,fog,blue' > ./file2.csv 

# Grab all lines from the CSV that match a pattern from file1:
% grep -if ./file1.txt ./file2.csv

# Grab all lines from the CSV that DON'T match a pattern from file1:
% grep -vif ./file1.txt ./file2.csv

Detailed explanation:

  • grep — self-explanatory
  • -v — means 'return lines not matching the input pattern'
  • -i — means 'use case-insensitive matching' (since your first file had capital letters and the CSV didn't)
  • -f — means 'interpret each line in the specified file (file1.txt) as a pattern to use for matching'

Depending on the results you want and the contents of your files, you may also want to read into the -F and -w options.

If you need to edit the file in-place, i think you can do this with sed's -f option, but sed interprets each line of the file as a command rather than a simple pattern like grep does.

share|improve this answer
Perfect, thanks. – Jason Yost May 10 '13 at 21:41

Your Answer


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.