组合多个正则表达式

我必须对几个字符串执行多个操作才能对其进行清理。我已经能够这样做,但是在 bash 中以

#   Getting the Content between START and END
    var1=$(sed '/START/,/END/!d;//d' <<< "$content")

#   Getting the 4th Line
    var2=$(sed '4q;d' <<< "$content")

#   Stripping all the new lines
    var1=${var1//$'\n'/}
    var2=${var2//$'\n'/}

#   Escaping the double quotes i.e. A"B => A\"B
    var1=$(sed "s/\"/\\\\\"/g" <<< "$var1")
    var2=$(sed "s/\"/\\\\\"/g" <<< "$var2")

#   Removing the contents wrapped in brackets i.e. A[...]B => AB
    var1=$(sed -e 's/\[[^][]*\]//g' <<< "$var1")
    var2=$(sed -e 's/\[[^][]*\]//g' <<< "$var2")

毫无疑问,当一次操作可以完成一次又一次的阅读时,这是非常糟糕的。 有什么建议吗?

工作示例:

SAMPLE INPUT

   [1][INTRO]
   [2][NAV]

   ABAQUEsnE,Masséot

... 

START

   French ceramist,who was the first grand-master of the glazed pottery
   at Sotteville-ls-Rouen (20 years before [8]bernard Palissy). He took
   part in the development of the ceramic factory of Rouen. He was the
   author - among others - of the ceramic triptych representing the Flood
   (1550,couen,Muse de la Renaissance).

END

DESIRED OUTPUT

ABAQUEsnE,Masséot
French ceramist,who was the first grand-master of the glazed pottery at Sotteville-ls-Rouen (20 years before bernard Palissy). He took part in the development of the ceramic factory of Rouen. He was the author - among others - of the ceramic triptych representing the Flood (1550,Muse de la Renaissance).
yeqishy 回答:组合多个正则表达式

您可以为此使用awk:

awk 'NR==4{sub(/^[[:blank:]]+/,""); print}' file

ABAQUESNE,Masséot

和第二个awk:

awk '{sub(/^[[:blank:]]+/,"")}
/^START/{p=1; next}
/^END/{sub(/\[[^]]*\]/,"",s); gsub(/"/,"\\\\&",s); print s; p=0; next}
p{s = s $0}' file

French ceramist,who was the first grand-master of the glazed potteryat Sotteville-ls-Rouen (20 years before Bernard Palissy). He tookpart in the development of the ceramic factory of Rouen. He was theauthor - among others - of the ceramic triptych representing the Flood(1550,couen,Muse de la Renaissance).
本文链接:https://www.f2er.com/3167178.html

大家都在问