如何删除匹配的行和上一行? [英] How do I delete a matching line and the previous one?
本文介绍了如何删除匹配的行和上一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除匹配的行和之前的行.例如在下面的文件中,我需要删除第 1 行 &2.
I need delete a matching line and one previous to it. e.g In file below I need to remove lines 1 & 2.
我试过 "grep -v -B 1 "page.of." 1.txt我希望它不会打印匹配的行和上下文.
I tried "grep -v -B 1 "page.of." 1.txt and I expected it to not print the matchning lines and the context.
我尝试了 如何使用 sed 删除匹配的行及其上方和下方的行?但无法理解 sed 的用法.
I tried the How do I delete a matching line, the line above and the one below it, using sed? but could not understand the sed usage.
---1.txt--
**document 1** -> 1
**page 1 of 2** -> 2
testoing
testing
super crap blah
**document 1**
**page 2 of 2**
推荐答案
你想做一些与 给出的答案
sed -n '
/page . of ./ { #when pattern matches
n #read the next line into the pattern space
x #exchange the pattern and hold space
d #skip the current contents of the pattern space (previous line)
}
x #for each line, exchange the pattern and hold space
1d #skip the first line
p #and print the contents of pattern space (previous line)
$ { #on the last line
x #exchange pattern and hold, pattern now contains last line read
p #and print that
}'
并作为一行
sed -n '/page . of ./{n;x;d;};x;1d;p;${x;p;}' 1.txt
这篇关于如何删除匹配的行和上一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文