我有一个非常大的字符串,我需要格式化,以便每行显示不超过16个字节。我使用for循环上升了16个字节,然后脚本停止解析剩余的字符串。我需要在第16个字节后添加一个新行,然后在下一行继续下一个16个字节。
$ echo "45a000531ee1000036118b9472041523cb5c8701084b084b003ff3443213002f8445f0a4a3d7000001800e291053dbe7157faa3ea02f850004720415238500047cc3685387000c020b921f7196979774f9000000"
| sed 's/../ &/g' | sed 's/^ //'
| awk 'ORS="\n"{ for (i=1; i<17;i++) printf ("%s ", $i)}'
45 a0 00 53 1e e1 00 00 36 11 8b 94 72 04 15 23
解决方案:(礼貌:克里斯)
awk 'NF{gsub(/.{16}/,"&\n",$0);print $0}' text | sed 's/../ &/g' | sed 's/^ //g'
45 a0 00 53 1e e1 00 00
36 11 8b 94 72 04 15 23
cb 5c 87 01 08 4b 08 4b
00 3f f3 44 32 13 00 2f
84 45 f0 a4 a3 d7 00 00
01 80 0e 29 10 53 db e7
15 7f aa 3e a0 2f 85 00
04 72 04 15 23 85 00 04
7c c3 68 53 87 00 0c 02
0b 92 1f 71 96 97 97 74
f9 00 00 00
答案 0 :(得分:4)
尝试:
命令:
echo "yourLongString" | sed 's/.\{16\}/&\n/g'
输出:
45a000531ee10000
36118b9472041523
cb5c8701084b084b
003ff3443213002f
8445f0a4a3d70000
01800e291053dbe7
157faa3ea02f8500
0472041523850004
7cc3685387000c02
0b921f7196979774
f9000000
编辑:
或者gawk:
gawk 'NF{gsub(/.{16}/,"&\n",$0);print $0}'
HTH Chris
答案 1 :(得分:2)
fold
折叠长行:
fold -b16 | sed 's/../& /g'