从文件中删除行
我有一个与此类似的问题: 使用 SED 或 AWK 从文件中删除行
如何删除以 3 和 5 开头的行以及此文件中的所有内容:
ssadfsadfsdf
asdfsadf
asdfsadf
asdfsadf
asdfsdf
1 z z z lkj klj lkj
2 satan er kaerleikur lkj lkj lkj
3 z z z
4 asdflj asd sdf
5 z z z
6 asdf sdaf asdf
7 z z z lkj lkj lkj
8 sdf safd asdf
9 z z z
如果答案明确在其他地方,我将删除此问题。
如果它看起来像这样怎么办:
asd ssadfsadfsdf
asd asdfsadf
asd asdfsadf
asd asdfsadf
asd asdfsdf
asd 1 z z z lkj klj lkj
asd 2 satan er kaerleikur lkj lkj lkj
asd 3 z z z
asd 4 asdflj asd sdf
asd 5 z z z
asd 6 asdf sdaf asdf
asd 7 z z z lkj lkj lkj
asd 8 sdf safd asdf
asd 9 z z z
I have a question similar to this one:
Delete lines from file with SED or AWK
how can I delete rows that start with 3 and 5 and everything in between in this file:
ssadfsadfsdf
asdfsadf
asdfsadf
asdfsadf
asdfsdf
1 z z z lkj klj lkj
2 satan er kaerleikur lkj lkj lkj
3 z z z
4 asdflj asd sdf
5 z z z
6 asdf sdaf asdf
7 z z z lkj lkj lkj
8 sdf safd asdf
9 z z z
If the answer is clearly elsewhere I will delete this question.
what if it looked liked this:
asd ssadfsadfsdf
asd asdfsadf
asd asdfsadf
asd asdfsadf
asd asdfsdf
asd 1 z z z lkj klj lkj
asd 2 satan er kaerleikur lkj lkj lkj
asd 3 z z z
asd 4 asdflj asd sdf
asd 5 z z z
asd 6 asdf sdaf asdf
asd 7 z z z lkj lkj lkj
asd 8 sdf safd asdf
asd 9 z z z
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 awk 范围模式 跳过匹配模式 1 到模式 2 的行:
我对字段
$1
进行数值比较,而不是使用正则表达式,例如/^3/
因为它也会匹配“30”、“31”等。如果数字是该行的第二个字段(如您编辑的示例输入中所示),只需更改
$1 到
$2
。让我知道这是否适合您。You can use an awk range pattern to skip line matching pattern 1 up to pattern 2:
I do a numerical comparison on field
$1
instead of a regular expression such as/^3/
because that would also match '30', '31', etc.If the number is the second field of the line (as in your edited example input), just change
$1
to$2
. Let me know if this works for you.谷歌很快给了我这个答案:
http://www. cyberciti.biz/faq/sed-howto-remove-lines-paragraphs/
要确保它与您所说的“行开头”匹配,您可以使用
^
开始该模式 象征。如果您使用 Vim(内置 sed),您可以执行
range 命令
,例如:其中 d 是删除命令,
:/exp1/,/exp2/
范围。A quick Google gave me this as seconds answer:
http://www.cyberciti.biz/faq/sed-howto-remove-lines-paragraphs/
To make sure it's matching "beginning of line" like you said, you can start the pattern with the
^
symbol.If you're using Vim (which has sed built in) you can do a
range command
like:where d is the delete command, and
:/exp1/,/exp2/
the range.会为你做的。
请参阅下面的测试:
输出:
will do it for you.
see the test below:
output: