Your second sed
solution should work fine in almost every case, but here are some other solutions just because:
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.... !
line two is a completely empty line, line 4 has a space, line 6 has a tab
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.... !
As shown above, this will not remove the lines containing whitespace
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.... !
Note: the above solution does not appear to work with 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.... !
This solution should work with both GNU and 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.... !
This does not modify in place but if you have GNU awk you can use the following
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.... !
As shown above, this will not remove the lines containing whitespace. Additionally this will not modify the file in place
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.... !
As shown above, this will not remove the lines containing whitespace. Additionally this will not modify the file in place
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.... !
This will not modify the file in place but a redirect could be used to create a new file with the desired contents
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.... !
As shown above, this will not remove the lines containing whitespace. Additionally this will not modify the file in place
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.... !
To modify in place use the following:
perl -i.bak -n -e "print if /\S/" file.log
ed
solution. – jesse_b Jul 28 '18 at 17:24