Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 组合多个正则表达式_Regex_Bash_Sed - Fatal编程技术网

Regex 组合多个正则表达式

Regex 组合多个正则表达式,regex,bash,sed,Regex,Bash,Sed,我必须对几个字符串执行多个操作来清理它们。我已经能够这样做了,但在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 ne

我必须对几个字符串执行多个操作来清理它们。我已经能够这样做了,但在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")
所需输出

   [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
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, couen, Muse de la Renaissance).

您可以为此使用awk:

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

第二个awk:

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


谢谢,但是我想在两个不同的变量中存储输出,虽然我可以在另一个sed中流式传输输出,但是如果可以的话,从这里?另外,我也想转义
,忘了在示例中添加它。
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).