"grep -Ff file1 file2"は、file1の単語を含む行の代わりにfile2のすべての行を印刷します。

file1.txt2つのファイルがありますfile2.txt

file1.txt4000行の単一の文字列単語があり、498のfile2.txt文があります。

私はその中に内容が欲しく、その文字列がgrep file2print matchで見つかったら欲しいです。file1file1file2

grep -f試してみましたが、grep -Ffファイルの内容だけを印刷するだけです。

ファイル1.txt

something
somthingelse
maybe
ok
yes

ファイル2.txt

Hello there how are you
Here is another line ok
Nothing to see here maybe
Nope not here
yes 

期待される出力

Here is another line ok
Nothing to see here maybe
yes



 wc -l file1.txt file2.txt
 4000 file1.txt
  498 file2.txt
 4498 total

ベストアンサー1

file1asに空白行があることを確認し、そうである場合、その行に含まれる空の文字列はasのすべての行と一致しますfile2。そこに空行がありますfile1

$ cat file1
something
somthingelse
maybe

ok
yes

$ grep -Ff file1 file2
Hello there how are you
Here is another line ok
Nothing to see here maybe
Nope not here
yes

yes単一の空白文字を含む行は、上記の入力が与えられた行とまだ一致していないすべての行に一致するものと同じ出力を生成します。

両方のファイルがCRLF区切り文字(CSVと共通)を持つMSDOSテキスト形式の場合、そのCR文字には1行しかありません(MSDOS / Windowsでは空白行、Unixでは制御文字OK)。目的。

以下を使用する前に、空白行または空白行をフィルタリングできますfile1

grep '[^[:space:]]' file1 | grep -Ff - file2

MSDOSファイルの場合は、CRを削除することもできます。file1それ以外の場合は、word<CR>行末のifでのみ見つけることができます。file1file2

<file1 dos2unix | grep '[^[:space:]]' | grep -Ff - file2

おすすめ記事