使用或在多行sed替换中 [英] Using or in multiline sed replacement
问题描述
我对sed的看似简单的部分-或语句感到困惑.我编写了以下sed文件,该文件分析了一个事件文件,其中的事件标签之间封装了多个事件,然后将每个事件的输出分别打印在一行上:
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:~$
从上面可以看到,日志中共有8个事件,结果均为允许.现在的麻烦是可能会有多个结果值.结果值可以是阻止修改"或允许".我以为第二个sed中的(Blocked | Modified | Allowed)可以处理此问题,但我得到以下输出:
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:~$
那我想念什么?为什么最终sed中的或停止换行符替换?
So what am I missing? why does the or in the final sed stop the newline replacement?
预先感谢
A
推荐答案
您要编写
sed '/\(Allowed\|Blocked\|Modified\)/{N;s/\n/ /;}'
:)
这篇关于使用或在多行sed替换中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!