ファイル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など)では、デフォルトではファイルのように動作します。これにより、コマンド出力をcut
grep 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