sed 或 awk 删除模式匹配之间的行,不包括第二个标记的行 [英] sed or awk deleting lines between pattern matches, excluding the second token's line
问题描述
我有一个 sed 命令可以成功打印匹配两种模式的行:
I have a sed command which will successfully print lines matching two patterns:
sed -n '/PAGE 2/,/\x0c/p' filename.txt
我没有想到的是,我希望它打印从第一个标记开始的所有行,直到 第二个标记.\x0c
标记是大平面文件上的记录分隔符,我需要保持该行完整.
What I haven't figured out, is that I want it to print all the lines from the first token, up until the second token. The \x0c
token is a record separator on a big flat file, and I need to keep THAT line intact.
在两个令牌之间,数据完全可变,我没有可靠的锚点可以使用.
In between the two tokens, the data is completely variable, and I do not have a reliable anchor to work with.
[澄清]现在它打印 /PAGE 2/
和 /\x0c/
之间的所有行.我希望它打印 /PAGE 2/
直到记录中的下一个 /\x0c/
.
[CLARIFICATION]
Right now it prints all the lines between /PAGE 2/
and /\x0c/
inclusive. I want it to print /PAGE 2/
up until the next /\x0c/
in the record.
[测试数据] /x0c
将位于此记录的第一行的开头和最后一行的开头.
[test data] The /x0c
will be at the start of the first line, and the beginning of the last line of this record.
我需要删除记录的第一行,通过下一条记录开始之前的行.
I need to delete the first line of the record, through the line just before the beginning of the next record.
^L20-SEP-2006 01:54:08 PM Foobars College PAGE 2
TERM: 200610 Student Billing Statement SUMDATA
99999
Foo bar R0000000
999 Geese Rural Drive DUE: 15-OCT-2012
Columbus, NE 90210
--------------------------------------------------------------------------------
Balance equal to or greater than $5000.00 $200.00
Billing inquiries may be directed to 444/555-1212 or by
email to bursar@foobar.edu. Financial Aid inquiries should
be directed to 444/555-1212 or finaid@foobar.edu.
^L20-SEP-2006 01:54:08 PM Foobars College PAGE 1
[预期结果]
^L20-SEP-2006 01:54:08 PM Foobars College PAGE 1
文件中会有多个这样的记录.我只能依赖 /PAGE 2/
令牌和 /x0c/
令牌.
There will be multiple such records in the file. I can rely only on the /PAGE 2/
token, and the /x0c/
token.
[解决方案]:
在 Choruba 的带领下,我将他的命令编辑为:
Following Choruba's lead, I edited his command to:
sed '/PAGE [2-9]/,/\x0c/{/\x0c$/!d}'
大括号中的规则适用于任何包含^L
的行,并有选择地忽略它们.
The rule in the curly brackets was applying itself to any line containing a ^L
and was selectively ignoring them.
推荐答案
在 Choruba 的带领下,我将他的命令编辑为:
Following Choruba's lead, I edited his command to:
sed '/PAGE [2-9]/,/\x0c/{/\x0c$/!d}'
这篇关于sed 或 awk 删除模式匹配之间的行,不包括第二个标记的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!