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

我正在使用sed -n '/[test1]/,/[test2]/{/[test2]/!p}' test.txt > temp.txt ..如果我只想要最上面的select,但是我在底部之后。

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

但是我在[test2]之后,所以复制[test2]直到行尾。 所以产生这样的输出,

 [test2] 1 2 3 

试试这个命令:

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

编辑

通过扩展正则表达式标志( -r ),

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

是的, grep -n会输出行号并匹配,并处理它,我想出了以下内容

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

所以新文件的一个猫产生以下内容:

  cat test.txt [test2] 1 2 3 

btw, | bash | bashawk格式化的命令行重定向到bash以执行它们