user1116360
user1116360

Reputation: 422

Delete lines from file -- awk

I have a file file.dat containing numbers, for example

4
6
7

I would like to use the numbers of this file to delete lines of another file.

Is there any way to pass this numbers as parameters to awk and delete these lines of another file?

I have this awk solution, but do not like it too much...

awk 'BEGIN { while( (getline x < "./file.dat" ) > 0 ) a[x]=0; } NR in a { next; }1' /path/to/another/file

Can you suggest something more elegant?

Upvotes: 0

Views: 1035

Answers (2)

kev
kev

Reputation: 161984

using NR==FNR to test which file awk is reading:

$ awk '{if(NR==FNR)idx[$0];else if(!(FNR in idx))print}' idx.txt data.txt

Or

$ awk 'NR==FNR{idx[$0]; next}; !(FNR in idx)' idx.txt data.txt
  • put index in idx.txt
  • put data in data.txt

Upvotes: 4

jlliagre
jlliagre

Reputation: 30873

I would use sed instead of awk:

$ sed $(for i in $(<idx.txt);do echo " -e ${i}d";done) file.txt

Upvotes: 1

Related Questions