awk相互参照2ファイル

awk相互参照2ファイル

2つのファイルがあり、両方に2つの列があります。 2.txtに同じ列がある場合は、1.txtから行を削除したいと思います。

はい -

1.txt(含む)

example:test
example2:test2
example3:test3

2.txtには次のものが含まれます(含む)

example:example
example2:example
example3:example
example4:example
example5:example

したがって、1.txt の列 1 の項目は 2.txt に等しいので、この場合、予想される出力は次のようになります。

example4:example
example5:example

削除されたコンテンツ -

example:test
example2:test2
example3:test3

ベストアンサー1

問題の説明はより明確になるかもしれませんが、例によれば次のことをしたいと思います。

awk -F: 'NR==FNR { d[$1]=1; next } !($1 in d) { print $0 }' 1.txt 2.txt

この-F:パラメータは、「コロン文字 ':'を列区切り文字として扱う」ことを意味します。 (デフォルトでは、連続したawk空白は列区切​​り文字として扱われます。)

2番目のパラメータはawk次のプログラムです。

IF this line is from the first input file THEN
    In a dictionary named `d`, create an item whose key is the first column of the input line and whose value is 1
    Skip the rest of the program move on to process the next line

(because of the "skip to next line" above we only do this for lines from the second file)
IF the dictionary named `d` has no item whose whose key is the first column of this line THEN
    Print this line

おすすめ記事