简体   繁体   English

如何删除以相同字符开头的行?

[英]How to remove lines that begin with the same character?

I'm trying to clean up the output from someone else's script by removing the headers that have no content. 我正在尝试通过删除不包含任何内容的标头来清理其他人脚本的输出。

Output currently looks like this: 当前输出如下所示:

====== Header1 ======
====== Header2 ======
====== Header3 ======
information

I'm trying to remove the lines for Header1 and Header2, but not Header3. 我正在尝试删除Header1和Header2的行,但不删除Header3的行。 I found an awk command that removes all duplicate lines but the last that begin with the same character, so that helps for this issue, but causes a new problem when the 'information' bit is numerous lines that also begin with the same character (usually tabs). 我发现了一个awk命令,该命令会删除所有重复的行,但最后一行以相同字符开头的行,这样可以解决此问题,但是当“信息”位中有很多行也以相同字符开头时(通常是标签)。

Desired output post cleanup: 所需的输出后清理:

====== Header3 ======
information

Thanks 谢谢

This awk might work for you: 这个awk可能对您有用:

$ awk '/^===/{h=$0;p=0;next}!p{print h};{p=1}1' file
====== Header3 ======
information

Or as Glenn pointed out, this also works: 或正如Glenn所指出的,这也可行:

awk '/^===/{h=$0;next}h{print h;h=0}1' file

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

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