Sed和正则表达式用于匹配除了作为另一个模式的一部分之外的所有数字。

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

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.

huangapple
  • 本文由 发表于 2023年6月5日 09:14:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403031.html
匿名

发表评论

匿名网友

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

确定