多行 regexp 替換為無條件分支
假設我有一個名為 in.txt
的檔案:
$ cat in.txt
a
b
a
c
a
d
我只想用 deleted
替換 a\nc
,而不是 a\nb
或 a\nd
。
$ sed -e ':loop # create a branch/label named `loop`
$!{
N # append the next line of input into the pattern space
/\n$/!b loop # If it is not the last line go to the `loop` branch again
}
s/a\nc/"deleted"/' in.txt # do replacing in the pattern space
a
b
"deleted" # see! succeed
a
d