ファイル2にエントリがある場合は、ファイル1から重複したエントリを削除します。

ファイル2にエントリがある場合は、ファイル1から重複したエントリを削除します。

重複したアイテムを削除したいです。金利file1から金利にも存在しますfile2

入力するfile1

x1  y1
x2  y2
x3  y3
x4  y4
y1  x1
x5  y5
y3  x3
x6  y6
x5  y5

入力するfile2

y1  x1
y2  x2
y3  x3
y4  x4
x1  y1
y5  x5
x3  y3
y6  x6
x5  y5

希望の出力:

x1  y1
x2  y2
x3  y3
x4  y4
x5  y5
x6  y6

次のシェルスクリプトを使用しました。

awk 'FNR==NR {

   lines[NR,"col1"] = $1
   lines[NR,"col2"] = $2
   lines[NR,"line"] = $0
   next
    }

  (lines[FNR,"col1"] != $1) {($1 in lines)
    print lines[FNR,"line"]
    next
}' file1.txt file2.txt

ただし、次の出力が提供されます。

x1  y1
x2  y2
x3  y3
x4  y4
y1  x1
x5  y5
y3  x3
x6  y6

ベストアンサー1

まず、目的の出力は次のようになります。

y2  x2
y4  x4
y5  x5
y6  x6

どちらのファイルにも「x3 y3」と「x1 y1」が存在するため

file1の行を取得するには、次のようにします。

grep -v -f file1 file2

マニュアルページから

-v
--invert-match
 Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)

-f file
   --file=file
Obtain patterns from file, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)  

おすすめ記事