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
두 번째 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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다