简体   繁体   English

使用sed匹配模式并从行中删除到文件末尾

[英]Using sed to match a pattern and deleting from the line to the end of the file

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. 我到处寻找,但似乎找不到符合我需要的表达方式。

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

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

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. 使用前一个表达式的输出,我想从-----THIS STUFF IS USELESS-----删除到文件的末尾,包括在内。 It should produce (after going through the first expression): 它应该产生(在经过第一个表达式之后):

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

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 (eg GNU-specific) options. 我也愿意使用任何其他工具,只要它可以在任何其他POSIX系统上使用,并且不使用特定于版本(例如GNU特定)的选项。

The actual text can be found here: http://pastebin.com/CYBbJ3qr Note the change from -----THIS STUFF IS USELESS----- to -----BEGIN PGP SIGNATURE----- . 实际文本可以在这里找到: http-----THIS STUFF IS USELESS-----注意从-----THIS STUFF IS USELESS-----的改变-----THIS STUFF IS USELESS----------BEGIN PGP SIGNATURE-----

why not 为什么不

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

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.

output 产量

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


With GNU sed , you can do 使用GNU sed ,你可以做到

sed '/^-----THIS STUFF IS USELESS-----$/Q' file

where Q is similar to q quit command, but doesn't print the matching line 其中Q类似于q quit命令,但不打印匹配行

Instead of trying to figure out how to express what what you don't want, just print what you DO want: 而不是试图弄清楚如何表达你不想要的东西,只需打印你想要的东西:

awk -v RS= '/Files:/' file

EDIT: Given your modified input: 编辑:鉴于您修改输入:

awk '/^Files:$/{f=1} f; /^$/{f=0}' file

or: 要么:

awk '/^Files:$/{f=1} f; /^-----THIS STUFF IS USELESS-----$/{f=0}' file

if you prefer. 如果你更喜欢。

You can also use either of these: 您还可以使用以下任一方法:

awk '/^Files:$/,/^-----THIS STUFF IS USELESS-----$/' file
sed '/^Files:$/,/^-----THIS STUFF IS USELESS-----$/' file

but they are hard to extend later. 但他们以后很难延长。

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

脏工具刀grep版本:

cat your_output.txt | grep -B 99999999 "THIS STUFF IS USELESS" | grep -v "THIS STUFF IS USELESS"

Here's a regular expression that I think will do what you want it to: ^(?:(?!Files:).)+|\\s*-----THIS STUFF IS USELESS-----.+ Make sure to set the dotall flag. 这是一个正则表达式,我认为它会做你想要的: ^(?:(?!Files:).)+|\\s*-----THIS STUFF IS USELESS-----.+确保设置dotall标志。

Demo+explanation: http://regex101.com/r/xF2fN5 演示+解释: http//regex101.com/r/xF2fN5

You only need to run this one expression. 您只需要运行这个表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM