加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix – 如何删除匹配的行和前一行?

发布时间:2020-12-15 18:58:22 所属栏目:安全 来源:网络整理
导读:我需要删除一个匹配的行和一个之前的行. 例如,在下面的文件中,我需要删除第1行和第1行. 2. 我试过“grep -v -B 1”page.of.“1.txt 我希望它不会打印匹配的行和上下文. 我尝试了How do I delete a matching line,the line above and the one below it,using
我需要删除一个匹配的行和一个之前的行.
例如,在下面的文件中,我需要删除第1行和第1行. 2.

我试过“grep -v -B 1”page.of.“1.txt
我希望它不会打印匹配的行和上下文.

我尝试了How do I delete a matching line,the line above and the one below it,using sed?,但无法理解sed的用法.

---1.txt--
**document 1**                         -> 1
**page 1 of 2**                        -> 2

testoing
testing

super crap blah

**document 1**
**page 2 of 2**
你想做一些与 answer given非常相似的事情
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读