本文介绍了如何使用sed/awk在十六进制数字之间添加零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文件包含该字符串
abc = <0x12345678>;
abc = <0x01234 0x56789>;
abc = <0x123 0x456 0x789>;
abc = <0x0 0x01234 0x0 0x56789>;
abc = <0x012 0x345>, <0x678 0x901>;
def = <0x12345 0x67890>;
我需要将其转换为文件包含
I need to convert it to file contains
abc = <0 0x12345678>;
abc = <0 0x01234 0 0x56789>;
abc = <0x123 0x456 0x789>;
abc = <0x0 0x01234 0x0 0x56789>;
abc = <0 0x012 0 0x345>, <0 0x678 0 0x901>;
def = <0x12345 0x67890>;
因此,如果字符串以'abc ='开头,则我需要在十六进制数字前添加零,即在两个三角括号之间不超过2个十六进制数字,并且没有 0x0之间的十六进制数字.如何使用 sed , awk 或其他bash工具做到这一点?
So I need to add zeros before HEX numbers if strings starts with 'abc = ', that are no more than 2 HEX numbers between couple of triangular brackets and there isn't 0x0 between that HEX numbers. How I can do it with sed, awk or another bash tools?
推荐答案
sed '/abc = </{/<[^ >]* [^ >]* /!s/0x/0 &/g}'
说明:
/abc = </{/<[^ >]* [^ >]* /!s/0x/0 &/g}
/abc = </ Operate on lines containing "abc = <"
{ } Group the commands together
! Negate the condition
/<[^ >]* [^ >]* / Match if lines contain more than two elements after <
s/0x/0 &/ Prepend "0 " to "0x"
g Globally, i.e. replace as many times as possible
这篇关于如何使用sed/awk在十六进制数字之间添加零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!