2012-01-08 28 views
0

我有一個文件file.dat包含數字,例如行 - AWK

4 
6 
7 

我想用這個文件的號碼刪除其它文件的行。

有沒有辦法將這些數字作爲參數傳遞給awk並刪除這些另一個文件的行?

我有這個awk解決方案,但不喜歡它太多...

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

您能否提供一些更優雅?

回答

4

使用NR==FNR測試哪些文件awk正在閱讀:

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

或者

$ awk 'NR==FNR{idx[$0]; next}; !(FNR in idx)' idx.txt data.txt 
  • 看跌指數idx.txt
  • 數據放在data.txt
+1

你可以用'next'在你的第一個行動,以避免使用'NR = FNR'作爲第二圖案! – 2012-01-08 11:13:37

+0

@JaypalSingh,好主意。謝謝 – kev 2012-01-08 11:31:12

1

我會用sed,而不是AWK:

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

'sed's /.*/& d /'idx.txt | sed -f - file.txt'可能不太忙。 – potong 2012-01-08 15:31:15