我在文件中有几行:
Desktop DELL firewall UP CPU 20core configured
Desktop HP firewall DOWN CPU 18core unconfigured我希望首先匹配UP,然后如果下一行匹配DOWN,则将这两行替换为某个字符串
我试过了
perl -p -e 's?.*firewall.*UP.*\n.*firewall.*DOWN.*?STRG?' file但它不起作用
发布于 2017-08-12 07:59:31
/\n.*f/不可能匹配,因为您一次只能读取一个以换行符结尾的行。
一个方便的技巧是使用-0777一次将整个文件加载到内存中。
perl -0777pe's/^.*firewall.*UP.*\n.*firewall.*DOWN.*/STRG/m' file发布于 2017-08-12 06:36:52
使用sed:
sed '/firewall.*UP/{N;/firewall[^\n]*DOWN/s/.*/somestr/}' file详细信息:
/firewall.*UP/ { # condition
N; # append the next line to the pattern space
/firewall[^\n]*DOWN/ # condition
s/.*/somestr/ # then: replace all with somestr
}https://stackoverflow.com/questions/45644444
复制相似问题