简体   繁体   English

如何使用sed删除所有行,直到第一列中的第一个匹配项?

[英]How to use sed to remove all lines until first match in first column?

How to use sed to remove all lines until first match in first column? 如何使用sed删除所有行,直到第一列中的第一个匹配项? The result should keep the last three lines. 结果应保留最后三行。

file 文件

1fff
2ddd
3zzz

 TestXXX
 TestXXX
Test        XXX
Mike
Charly

sed -i '/Test/' file sed -i'/ Test /'文件

sed -n '/^Test/,$p' oldfile > newfile

I'd use the .. operator in perl 我会在perl中使用..运算符
it toggles on when the first condition is met 满足第一个条件时打开
and toggles off when the second is met. 并在第二秒遇到时关闭。

(I've added ^ because you said you wanted the start of the line) (我添加了^,因为您说过您想从此行开始)

perl -nle 'print if /^Test/..0' file

LATE EDIT: on second reading, the ..0 isn't clear. 最新编辑:在二读时,.. 0不清楚。 it is the line number, and since line 0 will not be reached after "Test" it will never happen. 它是行号,由于在“测试”之后将不会到达行0,因此它将永远不会发生。

sed -n '/^Test/,$ p' YourFile

仅在测试后直到结束才打印

这可能对您有用(GNU sed):

sed -i '/^Test/,$!d' file

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

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