最初の列に基づいて2つのファイルを比較し、一致しないファイルを印刷します。

最初の列に基づいて2つのファイルを比較し、一致しないファイルを印刷します。

ファイル1:

test1,1
test2,2
test3

文書#2:

test2
test1
test4

希望の出力:

test4

ベストアンサー1

次の目的で使用できますgrep

$ grep -vwf <(cut -d, -f1 file1) file2
test4

説明する

  • grepオプション:

    -v, --invert-match
          Invert the sense of matching, to select non-matching lines.
    -w, --word-regexp
          Select  only  those  lines  containing  matches  that form 
          whole words.  
    -f FILE, --file=FILE
          Obtain patterns from FILE, one per line.  
    

    したがって、結合されたgrep -vwf patternFile inputFileのは、「inputFileで完全な単語として表示されないパターンファイルの行を見つけること」を意味します。

  • <(command):これをプロセス交換といい、これをサポートするシェル(bashなど)では、デフォルトではファイルのように動作します。これにより、コマンド出力をcutgrep optionsの「ファイル」として使用できます-f

  • cut -d, -f1 file1:file1の最初のカンマ区切りフィールドのみを印刷します。

データが実際に表示されているものと同じ場合-xにのみ使用するのではなく(全体の行と一致)を使用したい場合があります。-w

  -x, --line-regexp
          Select  only  those  matches  that exactly match the whole line.

だから:

$ grep -vxf <(cut -d, -f1 file1) file2
test4

またfile1、正規表現文字(など)を含めることができる.場合は、*?のものを使用することもできます-F

  -F, --fixed-strings
          Interpret PATTERNS as fixed strings, not regular expressions.

だから:

$ grep -Fvxf <(cut -d, -f1 file1) file2
test4

おすすめ記事