ワイルドカードのgrep結果に空白行が表示され、ファイルがCRLFで終わります。

ワイルドカードのgrep結果に空白行が表示され、ファイルがCRLFで終わります。

私が探したい用語(例:)とその周辺のテキストだけを表示するには、temperature次のように呼び出します。grep ".\{0,5\}temperature.\{0,5\}" *

このコマンドは、ファイルにUnix行末がある場合に正しく機能します。ただし、Windowsで生成されたファイル(CRLFで終わる)を検索するときにCRLFが5文字以内の場合、grepは正しい結果を表示するのではなく、行全体が空白として表示されます。

-U私は(バイナリ)フラグを試してみましたが、役に立ちませんでした。

私はテストに使用したファイルです。 CRLF(test_dos.txt)として保存されたバージョンとLF(test_unix.txt)としてのみ保存されたバージョンがあります。

the body temperature is the variable
temperature12345678
temperature1234567
temperature123456
temperature12345
temperature1234
temperature123
temperature12
temperature1
temperature

woie temperature!! tempe oaj

呼び出しの結果は次grep -o ".\{0,5\}temperature.\{0,5\}" *のとおりです。

test_dos.txt:body temperature is t
test_dos.txt:temperature12345
test_dos.txt:temperature12345
test_dos.txt:temperature12345
test_dos.txt:temperature12345





test_dos.txt:woie temperature!! te
test_unix.txt:body temperature is t
test_unix.txt:temperature12345
test_unix.txt:temperature12345
test_unix.txt:temperature12345
test_unix.txt:temperature12345
test_unix.txt:temperature1234
test_unix.txt:temperature123
test_unix.txt:temperature12
test_unix.txt:temperature1
test_unix.txt:temperature
test_unix.txt:woie temperature!! te

Windows / DOSファイルを使用してgrepに正しい結果を表示させるにはどうすればよいですか?

ベストアンサー1

grep他のオプションにエイリアスを指定しないでください。実験的には--color、GNUオプションはgrepDOSフォーマットファイルのCR処理をある程度妨げているように見えます。コマンドの前にプレフィックスを追加するか\(たとえば、\grep)実行可能ファイルへのフルパスを指定することで、/bin/grepエイリアス定義を上書きできます。

おすすめ記事