Вы можете сохранить предыдущую строку в удержании space:
sed '
/^pattern/{
x
/./ {
x
s/^/\
/
x
}
x
}
h'
Это было бы более разборчиво с awk
, хотя:
awk '!previous_empty && /pattern/ {print ""}
{previous_empty = $0 == ""; print}'
Как и GNU-реализации , sed
имеет параметр -i
для in-place редактирования, GNU-реализация awk
как -i inplace
для этого.
Эти имена имеют особое значение в bash, использование которых объясняется в руководстве:
If a sigspec is 0 or EXIT, arg is executed when the shell exits. If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function...
If a sigspec is ERR, the command arg is executed whenever a pipeline (which may consist of a single simple command), a list, or a compound command returns a non-zero exit status, subject to the following conditions...
Это не сигналы, но имеет смысл использовать механизм trap
и для некоторых других ситуаций. Эти имена имеют значение только внутри bash.