使用sed来匹配模式和从线中删除该文件的末尾 [英] Using sed to match a pattern and deleting from the line to the end of the file

查看:191
本文介绍了使用sed来匹配模式和从线中删除该文件的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图匹配来自管道输入和/或文件的图案,然后从匹配线删除该文件,包容的末尾。我看着到处都是,但似乎无法找到一个前pression,将适合我的需要。

I'm trying to match a pattern from piped input and/or a file, and then remove from the matched lines to the end of the file, inclusive. I've looked everywhere, but can't seem to find an expression that would fit my needs.

下面前pression让我消除了流的开头,包括匹配模式:

The following expression allows me to remove to the beginning of the stream, including the matched pattern:

sed -e '1,/Files:/d'

由于一些样本数据:

Given some sample data:

Blah blah blah
Foobar foo foo
Files:
 somefiles.tar.gz 1 2 3
 somefiles.tar.gz 1 2 3

-----THIS STUFF IS USELESS-----
BLEH BLEH BLEH
BLEH BLEH BLEH

运行上面的前pression生产:

Running the above expression produces:

Files:
 somefiles.tar.gz 1 2 3
 somefiles.tar.gz 1 2 3

-----THIS STUFF IS USELESS-----
BLEH BLEH BLEH
BLEH BLEH BLEH

我想达到类似的效果,但在相反的方向。使用从previous前pression输出,我想从删除-----这东西是无用----- 来结束该文件的,包容。它应该产生(通过第一八佰伴pression会后):

I would like to achieve a similar effect, but in the opposite direction. Using the output from the previous expression, I want to remove from -----THIS STUFF IS USELESS----- to the end of the file, inclusive. It should produce (after going through the first expression):

Files:
 somefiles.tar.gz 1 2 3
 somefiles.tar.gz 1 2 3

我也开到使用任何其他工具,只要它是可用的任何其他的POSIX系统上,并且不使用版本特定(例如GNU特异性)的选项。

I'm also open to using any other tools, as long as it is available on any other POSIX system and does not use version specific (e.g. GNU-specific) options.

实际的文本可以在这里找到: http://pastebin.com/CYBbJ3qr
注意从的变化-----这东西是无用----- ----- BEGIN PGP签名---- -

The actual text can be found here: http://pastebin.com/CYBbJ3qr Note the change from -----THIS STUFF IS USELESS----- to -----BEGIN PGP SIGNATURE-----.

推荐答案

为什么不

 sed '/^-----THIS STUFF IS USELESS-----$/,$d' file

在一个范围内的前pression就像你用,'$'将指定到文件末尾的

In a range expression like you have used, ',$' will specify "to the end of the file"

1 is first line in file, 
$ is last line in file.

输出

Files:
 somefiles.tar.gz 1 2 3
 somefiles.tar.gz 1 2 3

IHTH

这篇关于使用sed来匹配模式和从线中删除该文件的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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