Вы МОЖЕТЕ сделать это с помощью GNU sed (для -E
, -z
и распознав \n
как символ новой строки) с:
$ sed -Ez 's/(---\n)(---|$)/\1None\n\2/g' file
---POLICIES WITH 172.25.22.16 AS SOURCE ADDRESS---
None
---POLICIES WITH 172.25.22.16 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.17 AS SOURCE ADDRESS---
some output
---POLICIES WITH 172.25.22.17 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.18 AS SOURCE ADDRESS---
some output
---POLICIES WITH 172.25.22.18 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.19 AS SOURCE ADDRESS---
None
или GNU awk для RS с несколькими символами и gensub ():
$ awk -v RS='^$' -v ORS= '{$0=gensub(/(---\n)(---|$)/,"\\1None\n\\2","g")}1' file
---POLICIES WITH 172.25.22.16 AS SOURCE ADDRESS---
None
---POLICIES WITH 172.25.22.16 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.17 AS SOURCE ADDRESS---
some output
---POLICIES WITH 172.25.22.17 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.18 AS SOURCE ADDRESS---
some output
---POLICIES WITH 172.25.22.18 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.19 AS SOURCE ADDRESS---
None
но я бы настоятельно рекомендовал использовать вместо этого следующий POSIX awk для переносимости, а не для чтения всего файла в память сразу и возможности его простого улучшения, если вам необходимо внести какие-либо изменения:
$ cat tst.awk
/^---/ { if (NR>1) prt(); hdr=$0; next }
{ txt = txt ORS $0 }
END { prt() }
function prt() {
print hdr (txt=="" ? ORS "None" : txt)
hdr = txt = ""
}
$ awk -f tst.awk file
---POLICIES WITH 172.25.22.16 AS SOURCE ADDRESS---
None
---POLICIES WITH 172.25.22.16 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.17 AS SOURCE ADDRESS---
some output
---POLICIES WITH 172.25.22.17 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.18 AS SOURCE ADDRESS---
some output
---POLICIES WITH 172.25.22.18 AS DESTINATION ADDRESS---
None
---POLICIES WITH 172.25.22.19 AS SOURCE ADDRESS---
None