Text-Processing

替換包含換行符的字元串

  • October 23, 2014

使用bash外殼,在具有如下行的文件中

first "line"
<second>line and so on

我想每次替換一次或多次出現"line"\n<second>other characters獲取:

first other characters line and so on

所以我必須用特殊字元替換一個字元串,比如"and<和一個換行符。

在其他答案之間進行搜尋後,我發現sed可以在命令的右側(所以,other characters字元串)接受換行符,但不能在左側接受。

有沒有辦法(比更簡單)用sedor獲得這個結果grep

好吧,我可以想到幾種簡單的方法,但都不涉及grep(無論如何都不做替換)或sed.

  1. Perl

要將每次出現的"line"\n<second>替換為other characters,請使用:

$ perl -00pe 's/"line"\n<second>/other characters /g' file
first other characters line and so on

或者,要將多個連續出現的"line"\n<second>視為一個,並將它們全部替換為單個other characters,請使用:

perl -00pe 's/(?:"line"\n<second>)+/other characters /g' file

例子:

$ cat file
first "line"
<second>"line"
<second>"line"
<second>line and so on
$ perl -00pe 's/(?:"line"\n<second>)+/other characters /g' file
first other characters line and so on

導致 Perl 以“-00段落模式”讀取文件,這意味著“行”由\n\n而不是定義\n,本質上,每個段落都被視為一行。因此,替換匹配換行符。 2. awk

$  awk -v RS="\n\n" -v ORS="" '{
     sub(/"line"\n<second>/,"other characters ", $0)
     print;
   }' file 
first other characters line and so on

同樣的基本思想,我們將記錄分隔符 ( RS) 設置\n\n為 slurp 整個文件,然後將輸出記錄分隔符設置為空(否則會列印額外的換行符),然後使用該sub()函式進行替換。

讀取整個文件並進行全域替換:

sed -n 'H; ${x; s/"line"\n<second>/other characters /g; p}' <<END
first "line"
<second> line followed by "line"
<second> and last
END
first other characters  line followed by other characters  and last

引用自:https://unix.stackexchange.com/questions/163428