简体   繁体   English

使用sed删除行直到文件结尾

[英]Using sed to remove lines till the end of the file

I'm using sed -n '/[test1]/,/[test2]/{/[test2]/!p}' test.txt > temp.txt ..which works well if If I only want the top selection, but I'm after the bottom section. 我正在使用sed -n '/[test1]/,/[test2]/{/[test2]/!p}' test.txt > temp.txt ..如果我只想要顶部选择,那么效果很好但我在底部之后。

[test1]
A
B
C
[test2]
1
2
3

But I'm after [test2] onwards, so Copying [test2] till the end of the line. 但是我在[test2]之后,所以复制[test2]直到行结束。 So producing an output like so, 所以产生这样的输出,

[test2]
1
2
3

Try this command: 试试这个命令:

sed -n  '/\(^\[test2\]\)/,$p' test.txt > temp.txt

Edit : 编辑

With extended regular expression flag( -r ), 使用扩展正则表达式标志( -r ),

 sed -n -r '/(^\[test2\])/,$p' test.txt > temp.txt

Yes, grep -n will output the line number and match, and processing it I came up with the following 是的, grep -n将输出行号并匹配,并处理它我想出了以下内容

 grep -n '\[test2\]' temp.txt | awk -F\: '{print "sed \"1,"$1-1"d;\" temp.txt"}' | bash > test.txt

so a cat of the new file produces the following: 所以新文件的cat产生以下内容:

 cat test.txt
 [test2]
 1
 2
 3

btw, | bash 顺便说一下, | bash | bash redirects formatted command lines with awk to bash in order to execute them | bash将带有awk格式化命令行重定向到bash以执行它们

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

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