替换匹配行前面N行的内容
要求:sed 4.2.2+
https://unix.stackexchange.com/questions/206886/print-previous-line-after-a-pattern-match-using-sed
cat test.txt
status: done
memory: 1G
name: bwa_mem_S1
test: hehe
status: waiting
memory: 1G
name: bwa_mem_S2
test: hehe
status: waiting
memory: 1G
name: qc_S1
test: hehe
status: waiting
memory: 1G
name: qc_S2
test: hehe
cat test.txt | sed -zE 's#waiting(\n[^\n]*\nname: bwa_mem*)#done\1#g'
- -z: 一次性读取全部文件
- -E: 使用扩展的正则匹配模式,可以匹配换行符
- \1: 表示子组()匹配到的第一个