sh - 使用sed或awk修改文件并进行hex 2 dec转换

时间:2016-07-05 18:42:32

标签: awk sed sh

这是输入文件:

let controller = UIStoryboard(name: "Settings", bundle: nil).instantiateViewControllerWithIdentifier("")

我需要实现这个目标:

some data1 blah blah blah
some data2 blah blah bllah blah
Result HEX: 1ABC
Name......: Some name 1
-------------------------------------------
some random data1 blah blah blah bl
some random data2 blah blah bl
some random data3 blah blah blah blah bla
some random data4 blah blah blah bla
Result HEX: 2BCD
Name......: Whatever name 2
-------------------------------------------
some random data1 blah blah blah blah
some random data2 blah blah blah blah blah
some random data3 blah blah blah bl
Result HEX: 3CDE
Name......: Some name 3
-------------------------------------------

所以在文件匹配的每一行"结果HEX:"之后,我必须在"结果DEC:"附加一个新行。计算十六进制值到十进制。 我有一些小的嵌入式linux(基于busybox),所以我发现我可以使用这个命令转换hex2dec:

some data1 blah blah blah
some data2 blah blah bllah blah
Result HEX: 1ABC
Result DEC: 6844
Name......: Some name 1
-------------------------------------------
some random data1 blah blah blah bl
some random data2 blah blah bl
some random data3 blah blah blah blah bla
some random data4 blah blah blah bla
Result HEX: 2BCD
Result DEC: 11213
Name......: Whatever name 2
-------------------------------------------
some random data1 blah blah blah blah
some random data2 blah blah blah blah blah
some random data3 blah blah blah bl
Result HEX: 3CDE
Result DEC: 15582
Name......: Some name 3
-------------------------------------------

有人可以帮忙吗?......对我来说这是一个太大的挑战:( 谢谢!

1 个答案:

答案 0 :(得分:2)

gawk救援!

$ gawk --non-decimal-data '1; /HEX/{printf "%s DEC: %d\n", $1,"0x"$3}' file

some data1 blah blah blah
some data2 blah blah bllah blah
Result HEX: 1ABC
Result DEC: 6844
Name......: Some name 1
-------------------------------------------
some random data1 blah blah blah bl
some random data2 blah blah bl
some random data3 blah blah blah blah bla
some random data4 blah blah blah bla
Result HEX: 2BCD
Result DEC: 11213
Name......: Whatever name 2
-------------------------------------------
some random data1 blah blah blah blah
some random data2 blah blah blah blah blah
some random data3 blah blah blah bl
Result HEX: 3CDE
Result DEC: 15582
Name......: Some name 3
-------------------------------------------

您可以使用$2=="HEX"

替换模式来微调匹配