i have this command:

grep -Pvi ".(\.var|\.foo|\.varfoo)$" myfile.txt > newfile.txt

note: .var, .foo and .varfoo (and many others) are lines that are in myfile.txt.



The command works well and does what it has to do (eliminate any line that ends with .var, .foo, .varfoo, but keeping these 3). After executing the command:



Now, i need to put .var, .foo, .varfoo, in a "greplist.txt", to avoid putting them inside the command, so that "grep" reads it:



how do i do it?


Two challenges here:

  • -P only supports a single pattern
  • each line in your file must be a full pattern, not just the text itself

So if you set greplist.txt to


and just run

grep -vi -f greplist.txt myfile.txt > newfile.txt

you should get what you are looking for.

  • a question related. And if I wanted to do the opposite (keep .xxx.var, .yyy.foo, .zzz.varfoo in newfile.txt and delete .var .foo .varfoo). What should I put in the "greplist.txt" list (maybe ^\.var $ ...etc) ?? Thank you – ajcg Apr 22 '18 at 16:36
  • 1
    @user4839775 Omit the -v. See man grep for what this does. – nohillside Apr 22 '18 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.