Объединение нескольких выражений регулярных выражений - PullRequest
0 голосов
/ 04 ноября 2019

Мне нужно выполнить несколько операций над несколькими строками, чтобы очистить их. Я был в состоянии сделать это, но в нескольких операциях в bash as,

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

1 Ответ

1 голос
/ 04 ноября 2019

Для этого вы можете использовать awk:

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

ABAQUESNE, Masséot

и 2nd 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).
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...