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. 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 (e.g. GNU-specific) options.
我还可以使用任何其他的工具,只要它可以在任何其他POSIX系统上使用,并且不使用特定于版本的(例如特定于gnui的)选项。
The actual text can be found here: http://pastebin.com/CYBbJ3qr Note the change from -----THIS STUFF IS USELESS-----
to -----BEGIN PGP SIGNATURE-----
.
实际的文本可以在这里找到:http://pastebin.com/CYBbJ3qr注意从---这个东西是没用的----- --开始PGP签名-----。
5 个解决方案
#1
22
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
IHTH
IHTH
#2
4
sed -e '/^-----THIS STUFF IS USELESS-----$/,$ d'
#3
4
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.
但以后很难再延伸。
#4
1
Dirty utility knife grep
version:
肮脏实用刀grep版本:
cat your_output.txt | grep -B 99999999 "THIS STUFF IS USELESS" | grep -v "THIS STUFF IS USELESS"
#5
0
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.
这是一个正则表达式,我认为会做你想要的:^(?:? !文件:。)+ | \ s * - - - - - - - - - - - -这东西是无用的。+务必设置dotall旗帜。
Demo+explanation: http://regex101.com/r/xF2fN5
演示+解释:http://regex101.com/r/xF2fN5
You only need to run this one expression.
只需要运行这个表达式。
#1
22
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
IHTH
IHTH
#2
4
sed -e '/^-----THIS STUFF IS USELESS-----$/,$ d'
#3
4
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.
但以后很难再延伸。
#4
1
Dirty utility knife grep
version:
肮脏实用刀grep版本:
cat your_output.txt | grep -B 99999999 "THIS STUFF IS USELESS" | grep -v "THIS STUFF IS USELESS"
#5
0
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.
这是一个正则表达式,我认为会做你想要的:^(?:? !文件:。)+ | \ s * - - - - - - - - - - - -这东西是无用的。+务必设置dotall旗帜。
Demo+explanation: http://regex101.com/r/xF2fN5
演示+解释:http://regex101.com/r/xF2fN5
You only need to run this one expression.
只需要运行这个表达式。