1

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.

myfile.txt:

.var
.foo
.varfoo
.xxx.var
.yyy.foo
.zzz.varfoo

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:

newfile.txt

.var
.foo
.varfoo

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

greplist.txt

.var
.foo
.varfoo

how do i do it?

2

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

.\.var$
.\.foo$
.\.varfoo$

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.