繁体   English   中英

awk + ​​xxd bash 命令中关于 ASCII 换行字节的混淆

[英]confusion about ASCII linefeed byte in awk + xxd bash command

我对某些 bash 命令中发生的某些0a (即NL ASCII 字节)感到困惑。 在以下方面:

$ echo | sha1sum $1 | awk '{print $1;}' | xxd -r -ps > test.bin
$ echo | sha1sum $1 | awk '{print $1;}' > test.hex
$ xxd test.bin
00000000: adc8 3b19 e793 491b 1c6e a0fd 8b46 cd9f  ..;...I..n...F..
00000010: 32e5 92fc                                2...
$ xxd test.hex
00000000: 6164 6338 3362 3139 6537 3933 3439 3162  adc83b19e793491b
00000010: 3163 3665 6130 6664 3862 3436 6364 3966  1c6ea0fd8b46cd9f
00000020: 3332 6535 3932 6663 0a                   32e592fc.

是什么导致0a字节出现在test.hex但不在test.bin

注 1 :这是一个我一直在按照那里使用的解决方案问自己的问题:

在 bash 中以二进制格式将 ```sha``` 校验和输出转储到磁盘,而不是纯文本十六进制

注 2 :我能够抑制0a字节,这不是问题,我只是好奇为什么它存在于一种情况而不是另一种情况:

$ echo | sha1sum $1 | awk '{print $1;}' | head -c-1 > test_2.hex
$ xxd test_2.hex
00000000: 6164 6338 3362 3139 6537 3933 3439 3162  adc83b19e793491b
00000010: 3163 3665 6130 6664 3862 3436 6364 3966  1c6ea0fd8b46cd9f
00000020: 3332 6535 3932 6663                      32e592fc

您看到的0a来自awk 默认情况下, awk输出记录分隔符\\n并且您可以通过设置ORS (例如使用BEGIN {ORS=""} )来远程它。

由于-r参数,当您通过管道传输xxd -r -ps时会丢失它。 来自手册页:“任何地方都允许使用额外的空格和换行符。”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM