Skip to content
Advertisement

grep -v with while read line command not working properly

I use this command

while read line || [ -n "$line" ]; do grep -v "$line" target.txt > tmp; mv tmp target.txt; done < removefrom.txt

to remove lines from (target.txt) that match string in (removefrom.txt)


This is (target.txt)

e
1
k
2
a

This is (removefrom.txt)

d
e
c
a
k
b
e
a

This is the result should look like:

1
2

But sometimes the resule is like:

e
1
2

or

e
1
2
a

or

1
2
a

I ever try to use sed, but the problem is still there, It didn’t remove lines properly.

Advertisement

Answer

grep -vf removefrom.txt target.txt

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement