Using or in multiline sed replacement

大城市里の小女人 提交于 2020-01-03 04:09:06

问题


I'm confused with a seeming simple part of sed - the or statement. I wrote the following sed which parses an event file with multiple events encapsulated between event tags and then prints the output of each event on 1 line each:

machinename:~$ sed -n "/<event/,/<\/event>/ {/<result/{s/.*result value=\" \(.*\)\"\/>.*/\1/g; p}; /<sender/ {s/.*phoneNo=\"\(.*\)\" country.*/\1/g; p}}" xmlEventLog_2010-02-05T16:40:55.xml | sed '/Allowed/{N;s/\n/ /;}'
Allowed +989350000100
Allowed +989350000209
Allowed +989350000002
Allowed +989350000002
Allowed +989350000100
Allowed +989350000209
Allowed +989350000002
Allowed +989350000002
machinename:~$

From the above there are 8 events in the log all with result Allowed. Now the trouble is there could be more than one result value. Result values could be Blocked Modified or Allowed. I thought that (Blocked|Modified|Allowed) in the second sed would handle this but I got the following output:

machinename:~$ sed -n "/<event/,/<\/event>/{/<result/{s/.*result value=\"\(.*\)\"\/>.*/\1/g; p}; /<sender/ {s/.*phoneNo=\"\(.*\)\" country.*/\1/g; p}}" xmlEventLog_2010-02-05T16:40:55.xml | sed '/(Allowed|Blocked|Modified)/{N;s/\n/ /;}'
Allowed
+989350000100
Allowed
+989350000209
Allowed
+989350000002
Allowed
+989350000002
Allowed
+989350000100
Allowed
+989350000209
Allowed
+989350000002
Allowed
+989350000002
machinename:~$

So what am I missing? why does the or in the final sed stop the newline replacement?

Thanks in advance

A


回答1:


You want to write

sed '/\(Allowed\|Blocked\|Modified\)/{N;s/\n/ /;}'

:)



来源:https://stackoverflow.com/questions/2826049/using-or-in-multiline-sed-replacement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!