英文:
Sed and regex to match all number except as part of another pattern
问题
Desired output:
Execution error for request #########. JBO-12345: Reason: Expected meta model version with uuid '########-####-####-####-############', but retrieved meta model version with uuid '########-####-####-####-############'
英文:
I need to use sed to match replace number patterns, but only if they aren't part of something else. Example:
Input
echo "Execution error for request 40205911. JBO-12345: Reason: Expected meta model version with uuid '34a659f5-ad37-4fea-9593-95a1a818adbb', but retrieved meta model version with uuid '8a3de0d2-fcd9-4bfd-b4a7-070679dc0184'"
sed file:
s/([0-9a-f]{9}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/########-####-####-####-############/g
s/[0-9]{5,8}/########/g
s/[0-9]/#/g
Desired output:
Execution error for request ########. JBO-12345: Reason: Expected meta model version with uuid '########-####-####-####-############', but retrieved meta model version with uuid '########-####-####-####-############'
But hat I am getting is:
Execution error for request ########. JBO-###: Reason: Expected meta model version with uuid '##a###f#-ad##-#fea-####-##a#a###adbb', but retrieved meta model version with uuid '#a#de#d#-fcd#-#bfd-b#a#-########dc####
I've some examples that describe (I think) what I need; a look ahead reference
答案1
得分: 2
使用 sed
:
$ sed -E ":a;s/( |#)[0-9]|('[^,]*)[[:alnum:]]/#/;ta;" input_file
创建标签 a
,以便在模式匹配成功时回到:
- 匹配一个空格后跟一个整数
- 匹配一个
#
后跟一个整数 - 匹配一个单引号
'
直到逗号,删除所有中间的字母数字字符,每次找到匹配时替换为#
。
英文:
Using sed
$ sed -E ":a;s/( |#)[0-9]|('[^,]*)[[:alnum:]]/#/;ta;" input_file
Execution error for request ########. JBO-12345: Reason: Expected meta model version with uuid '########-####-####-####-############', but retrieved meta model version with uuid '########-####-####-####-############'
Create a label a
to branch back to if a substitution is successful when the pattern:
space followed by an integer
hash
# followed by an integer
A single quote
' up to a comma removing all alphanumeric characters in between
is found then replace with a #
hash each time a match is found.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论