0

I have 2 files : One.lst and Two.lst.

One.lst contains :

a
b
c
d
e

Two.lst contains :

c
d

I need One.lst to contain only a,b,e ie removing the lines from One.lst that are already present in Two.lst.

So,the updated One.lst will be :

a
b
e
fedorqui
  • 275,237
  • 103
  • 548
  • 598
6055
  • 439
  • 1
  • 4
  • 14

1 Answers1

0

This is a job for grep:

$ grep -vf Two.lst One.lst
a
b
e

To update the file with the content, do:

grep -vf Two.lst One.lst > temp_file && mv temp_file One.lst

From man grep:

-f FILE, --file=FILE

Obtain patterns from FILE, one per line.

-v, --invert-match

Invert the sense of matching, to select non-matching lines.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • No, you cannot redirect `grep`'s output to one of the files that is involved in the operation. Otherwise you will get an error like "grep: input file ‘One.lst’ is also the output" – fedorqui Apr 08 '14 at 09:50
  • The files that i have(One.lst and Two.lst) are pretty huge in size and i already have space issues.So,i cannot create another temp_file.Is there any other way? – 6055 Apr 08 '14 at 09:53
  • Well you can maybe loop through `Two.lst` and for every line, delete it from `One.lst` with `sed -i`. It won't require temp files, but will be very unefficient. – fedorqui Apr 08 '14 at 09:57
  • Yes,that would be inefficient.Anyway,i will try with your approach only. Thanks a lot for the help. Highly appreciated. – 6055 Apr 08 '14 at 09:58