file1.txt
2つのファイルがありますfile2.txt
。
file1.txt
4000行の単一の文字列単語があり、498のfile2.txt
文があります。
私はその中に内容が欲しく、その文字列がgrep
file2
print matchで見つかったら欲しいです。file1
file1
file2
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
file1
asに空白行があることを確認し、そうである場合、その行に含まれる空の文字列は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でのみ見つけることができます。file1
file2
<file1 dos2unix | grep '[^[:space:]]' | grep -Ff - file2