sed command to locate a string containing alphanumeric characters and spaces and perform positional replacement in that string

huangapple go评论58阅读模式
英文:

sed command to locate a string containing alphanumeric characters and spaces and perform positional replacement in that string

问题

我想要定位 ARF = 0x000000006e1d9000 并在位置12、13、14等特定位置执行替换,最终结果应该如下:

aadz(mdb awetyi),Lll,Dweuio: mdb 1:00.0  ARF = 0x000000006xxx000

我尝试了这个命令:

sed -i -e 's/\(.\{12\}\)./x/' -e 's/\(.\{13\}\)./x/' -e 's/\(.\{14\}\)./x/' file

但它替换了包含文本的行的开头字符,这不是期望的行为。我无法只定位 ARF = 0x000000006xxx000

英文:

i have a file with below line

aadz(mdb awetyi),Lll,Dweuio: mdb 1:00.0  ARF = 0x000000006e1d9000

i want to locate ARF = 0x000000006e1d9000 and perform positional replacement of certain positions like 12,13,14 etc with x. the final result should look like

aadz(mdb awetyi),Lll,Dweuio: mdb 1:00.0  ARF = 0x000000006xxx000

i tried this command

sed -i -e 's/\(.\{12\}\)./x/' -e  's/\(.\{13\}\)./x/'  -e  's/\(.\{74\}\)./x/' file

but it replaces the characters from start of the line containing the text which is not desired
I'm unable to locate only ARF = 0x000000006xxx000

答案1

得分: 2

你可以使用以下命令执行此操作:

sed -E 's/(ARF = .{11}).{3}/xxx/g' file

它匹配ARF = ,接下来的11个字符,并将接下来的三个字符替换为xxx

请注意,示例中未包含-i选项:首先测试替代是否按您所需的方式进行。

英文:

You can do it with the following command

sed -E 's/(ARF = .{11}).{3}/xxx/g' file

It matches ARF = , 11 following symbols and replaces following three symbols with xxx

Option -i is not included in example: first test to see that replacement is happening like you want.

huangapple
  • 本文由 发表于 2023年4月11日 15:44:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定