簡體   English   中英

刪除具有特定模式的行

[英]delete lines with specific pattern

嗨,我必須刪除文件中的一些行:

file 1
1 2 3
4 5 6

file 2
1 2 3 6
5 7 8 7
4 5 6 9

我必須刪除文件2中找到的文件1的所有行:

output
5 7 8 7

我用過sed:

for sample_index in $(seq 1 3)
do
  sample=$(awk 'NR=='$sample_index'' file1)
  sed "/${sample}/d" file2 > tmp
done

但它不起作用。它不會打印任何東西。 你有什么想法嗎?它給我錯誤'sed:-e expression#1,char 0:需要先例正則表達式'

這可能是一個開始:

$ grep -vf file1 file2
5 7 8 7

這里的一個潛在缺陷是,如果你把5 6 9作為file1的第二行,輸出不會改變。 我不確定你是否想要那個。 如果沒有,你可以試試

grep -vf <(sed 's/^/^/' file1) file2

如果您的真實數據為3列,這應該有效:

awk 'NR==FNR{a[$1$2$3]++;next}!($1$2$3 in a)' file{1,2}

對於變量列:

awk 'NR==FNR{a[$0]++;next}{for(x in a) if(index($0,x)>0) next}1' file{1,2}

和GNU 的代碼

sed -r 's#(.*)#/\1/d#' file1 | sed -f - file2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM