删除两个方向的n行和sed中的匹配? [英] Deleting n lines in both directions and the match in sed?

查看:54
本文介绍了删除两个方向的n行和sed中的匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在它起作用之前删除匹配和两行:

Deleting the match and two lines before it works:

sed -i.bak -e '/match/,-2d' someCommonName.txt

运行后删除匹配和两行:

Deleting the match and two lines after it works:

sed -i.bak -e '/match/,+2d' someCommonName.txt

但是删除匹配后两行和前两行不起作用?

But deleting the match, two lines after it and two lines before it does not work?

sed -i.bak -e '/match/-2,+2d' someCommonName.txt
sed: -e expression #1 unknown command: `-'

这是为什么?

推荐答案

我没有完整的解决方案,只有一个大纲:sed 是一个非常简单的工具,它不做两件事一次.我的方法是运行 sed 一次删除模式后的两行但保留模式本身.然后可以将结果再次通过管道传送到 sed 以删除模式和之前的两行.

I don't have a complete solution but an outline: sed is a pretty simple tool which doesn't do two things at once. My approach would be to run sed once deleting the two lines after the pattern but keeping the pattern itself. The result can then be piped to sed again to remove the pattern and the two lines before.

这篇关于删除两个方向的n行和sed中的匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆