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
但它不工作
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
但它不工作
帶S編輯:
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
}
這可能會創建一個假陽性,如果' DOWN'與'UP'在同一行? – potong
@potong:顯然是的,但我懷疑這可能發生。但是我會用'[^ \ n] *'替換第二個'。*',以確保*防火牆*和* DOWN *位於同一行(第二個)。 –
/\n.*f/
不可能匹配,因爲你正在閱讀換行符終止行一次一個。
一個方便的技巧是使用-0777
一次將整個文件加載到內存中。
perl -0777pe's/^.*firewall.*UP.*\n.*firewall.*DOWN.*/STRG/m' file
可能重複:[?如何替換多個任意字符(包括換行)在Perl的正則表達式(https://stackoverflow.com/q/36533282/2173773) –