在与 sed 匹配之前删除所有行 [英] Remove all lines before a match with sed

查看:53
本文介绍了在与 sed 匹配之前删除所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sed 来过滤文件列表.我有一个排序的文件夹列表,我想在特定行之后获取所有行.要完成此任务,我使用了此处 中描述的解决方案,该解决方案适用于我尝试过的任何输入,但没有当比赛在第一行时工作.在这种情况下 sed 将删除输入的所有行

这是一个例子:

$ ls -1/垃圾桶开机...系统时间用户无功vmlinuz$ ls -1/|sed '1,/tmp/d'用户无功vmlinuz$ ls -1/|sed '1,/^bin$/d'# sed 将删除输入流中的所有行

当第一行与正则表达式匹配时,我应该如何更改命令以考虑极限情况?<​​/p>

顺便说一句 sed '1,1d' 正常工作,只删除第一行.

解决方案

试试这个(仅限 GNU sed):

sed '0,/^bin$/d'

..输出是:

<前>$sed '0,/^bin$/d' 文件开机...系统时间用户无功vmlinuz

I'm using sed to filter a list of files. I have a sorted list of folders and I want to get all lines after a specific one. To do this task I'm using the solution described here which works pretty well with any input I tried but it doesn't work when the match is on the first line. In that case sed will remove all lines of the input

Here it's an example:

$ ls -1 /
bin
boot
...
sys
tmp
usr
var
vmlinuz

$ ls -1 / | sed '1,/tmp/d'
usr
var
vmlinuz

$ ls -1 / | sed '1,/^bin$/d'
# sed will delete all lines from the input stream

How should I change the command to consider also the limit case when first line is matched by regexp?

BTW sed '1,1d' correctly works and remove the first line only.

解决方案

try this (GNU sed only):

sed '0,/^bin$/d'

..output is:

$sed '0,/^bin$/d' file
boot
...
sys
tmp
usr
var
vmlinuz

这篇关于在与 sed 匹配之前删除所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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