bash 使用 sed 删除行直到文件末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6482067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using sed to remove lines till the end of the file
提问by user349418
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如果我只想要顶部选择,但我在底部之后,我正在使用..which 效果很好。
[test1]
A
B
C
[test2]
1
2
3
But I'm after [test2] onwards, so Copying [test2] till the end of the line. So producing an output like so,
但是我在 [test2] 之后,所以复制 [test2] 直到行尾。所以产生这样的输出,
[test2]
1
2
3
回答by Prince John Wesley
Try this command:
试试这个命令:
sed -n '/\(^\[test2\]\)/,$p' test.txt > temp.txt
Edit:
编辑:
With extended regular expression flag(-r),
使用扩展的正则表达式 flag( -r),
sed -n -r '/(^\[test2\])/,$p' test.txt > temp.txt
回答by matchew
Yes, grep -nwill 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"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, | bashredirects formatted command lines with awkto bashin order to execute them
顺便说一句,| bash重定向与格式化的命令行awk到bash为了执行它们

