简体   繁体   English

使用sed删除空白行之前的所有内容

[英]Remove everything before a blank line using sed

Lets say I have a file which is something like this: 可以说我有一个像这样的文件:

"Testing is important"

Nothing is impossible

The output should be: 输出应为:

Nothing is impossible

This means the sed removed everything before new line. 这意味着sed删除了新行之前的所有内容。 Also, I need to make sure it works on bash on windows. 另外,我需要确保它可以在Windows的bash上运行。

Please help. 请帮忙。

You can try this 你可以试试这个

sed '1,/^\s*$/d' file  

\\s is whitespace, it's same with \\ s是空格,与

sed '1,/^[[:blank:]]*$/d' file  

Sed supports addressing lines both as numbers and as matching regex. Sed支持将行寻址为数字和匹配的正则表达式。 In your case, you can delete all lines starting from 1, and ending with an empty line: 您可以删除从1开始并以空行结尾的所有行:

sed -e '1,/^$/d'

On Windows your files may contain contain carriage returns, in which case you can use: 在Windows上,您的文件可能包含回车符,在这种情况下,您可以使用:

sed -e '1,/^\r*$/d'

(assuming GNU sed) (假设GNU sed)

To allow for more than one blank line and multiple lines after the final blank line, you could use something like this: 要允许一个以上的空行和最后一个空行之后的多行,可以使用如下所示的内容:

awk 'BEGIN{RS=ORS=""}{a=$0}END{print a}' file

This unsets the Record Separator RS , so that each block/paragraph is treated as a separate record. 这将取消Record Separator RS ,因此每个块/段落都被视为单独的记录。 It assigns each record to the variable a , then prints the last value of a once the file has been processed. 它给每个记录的变量a ,然后打印的最后一个值a一旦该文件已被处理。 The Output Record Separator ORS is also unset, so that no newline is appended to the final block. 输出记录分隔符ORS也未设置,因此没有换行符附加到最后一个块。

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

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