我正在努力使用sed处理“ testfile.txt”,并且每次遇到以delete_me: abc
开始的行时,它都会:
delete_me: abc
行例如我想要此输入:
delete_me: abc
sSAsaAaSA
AsaSAsaSAsa
asASAsS
^--- <blank line>
...仅更改为这一行:
delete_me: abc
我尝试过:
sed '/delete_me/ {n;d}' jil_testfile.txt
# deletes only the first line after 'delete_me'
sed '/delete_me/,/^$/d' jil_testfile.txt
# nearly works but deletes the 'delete_me' line too which I want to stay preserved.
有什么建议吗?
答案 0 :(得分:1)
这可能对您有用(GNU sed):
sed -n ':a;/delete_me/{p;:b;n;//ba;bb};p' file
正常打印行,直到第一次出现delete_me
为止。打印此行,除非该行包含delete_me
,否则不要再打印其他行。
自从我编写第一个解决方案以来,规范发生了变化,这是新的解决方案:
sed -n '/delete_me/{p;:a;n;/^$/b;ba};p' file