sedを使用してファイルリストから文字列を削除できませんでした。

sedを使用してファイルリストから文字列を削除できませんでした。

私はbash(Mac OS X)を使用しています。削除したい文字列を含むファイルのリストがあります。

$ grep -l \</html\> *.html  
21888601.html  
21906283.html  
21977081.html  
...

一致するすべてのファイルの名前はこの形式(.html)で指定されます。それからこれを試してください。

$ grep -l \</html\> 27776977.html | xargs -0 sed -i.back '/<\/html>/d'

シェルはgrepから返されたファイルのリストとエラーを出力します。

sed: 21888601.html  
21906283.html  
21977081.html  
...
: File name too long

このファイル名は明らかに長すぎないため、ここには別のエラーがあります。また、アルファベット名(すべての数字ではない)を持つファイルでこれをテストしてもエラーは発生しません。

私も次のことを試しました。

$ grep -l \</html\> 27776977.html | xargs -0 sed -i.back '/<\/html>/d'
sed: 27776977.html
: No such file or directory

$ grep -l \</html\> 27776977.html
27776977.html

sedは数値ファイル名を処理できませんか?それともここに他の質問がありますか?

ベストアンサー1

-0このオプションを使用するため、xargs入力ファイル名を終了するためにスペースの代わりにヌル文字を探します。これにより、見つかったすべてのファイルがgrep個々のファイルではなく1つの長い文字列にリンクされます。

詳細については、以下を参照してくださいman xargs

-0, --null
              Input items are terminated by a null character instead of by whitespace,  and  the  quotes  and
              backslash  are  not  special  (every  character  is taken literally).  Disables the end of file
              string, which is treated like any other argument.  Useful when input items might contain  white
              space,  quote  marks,  or backslashes.  The GNU find -print0 option produces input suitable for
              this mode.

この場合、ファイル名に特殊文字がないため、その-0オプションを削除する必要があります。

おすすめ記事