file.log:

file.log:

削除に適したコマンドは何ですか?空行16進ダンプファイルから部品を互いに接着しますか?

sed -i '/^$/d' file.log    
sed -i '/^\s*$/d' file.log

それとも多分awk

801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d
801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8
801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !
801c3ff0: 0c0a 8935  2405 0002  5040 0064  0000 1021 | [email protected]...!


801c4000: 0200 2021  0c0a 8935  2405 0002  0200 2021 | .. !...5$..... !
801c4010: 0040 2821  2406 0002  3c07 8074  0c0a 86a5 | .@(!$...<..t....
801c4020: 24e7 10fc  0040 2021  3c05 8074  0c1b 634c | $....@ !<..t..cL

ベストアンサー1

2番目sedの解決策はほとんどすべての場合にうまく機能しますが、次の理由で他の解決策があります。

file.log:

$ cat file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....

Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d

Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8

Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

2行目は完全に空白で、4行目にはスペースがあり、6行目にはタブがあります。


sed:

sed '/^$/d' file.log

$ sed '/^$/d' file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d

Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8

Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

上記のように、スペースを含む行は削除されません。

sed '/^\s*$/d' file.log

$ sed '/^\s*$/d' file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d
Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8
Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

注:上記の回避策はBSD sedでは機能しないようです。

sed '/^[[:space:]]*$/d' file.log

$ sed '/^[[:space:]]*$/d' file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d
Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8
Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

このソリューションはGNUとBSD sedの両方で動作します。


awk:

awk 'NF' file.log

$ awk 'NF' file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d
Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8
Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

内部変更は行われませんが、GNU awkがある場合は、次のコマンドを使用できます。

awk -i inplace 'NF' file.log

grep:

grep -v '^$' file.log

$ grep -v '^$' file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d

Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8

Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

上記のように、スペースを含む行は削除されません。また、これはファイルを変更しません。

grep . file.log

$ grep . file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d

Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8

Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

上記のように、スペースを含む行は削除されません。また、これはファイルを変更しません。

grep -v '^\s*$' file.log

$ grep -v '^\s*$' file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d
Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8
Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

これによりファイルは変更されませんが、リダイレクトを使用して目的のコンテンツを含む新しいファイルを作成できます。


tr:

tr -s '\n' <file.log

$ tr -s '\n' <file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d

Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8

Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

上記のように、スペースを含む行は削除されません。また、これはファイルを変更しません。


perl:

perl -n -e "print if /\S/" file.log

$ perl -n -e "print if /\S/" file.log
801c3fb0: 0000 2821  0c18 9741  2406 0020  afb0 0010 | ..(!...A$.. ....
Empty line before-801c3fc0: 2402 0014  afa2 0038  8e22 00e4  2404 0064 | $......8."..$..d
Space before-801c3fd0: 0000 2821  03a0 3021  0040 f809  27a7 0038 | ..(!..0!.@..'..8
Tab before-801c3fe0: 1040 001d  0040 9021  2630 0008  0200 2021 | .@...@.!&0.... !

現在の場所から変更するには、次のコマンドを使用します。

perl -i.bak -n -e "print if /\S/" file.log

おすすめ記事