简体   繁体   English

在文本文件中,如何删除紧随其后的行的所有行子集?

[英]In a text file, how do I delete all lines subset of their immediately following line?

In a text file, I need to delete lines that are exactly beginning like the next line.在文本文件中,我需要删除与下一行完全相同的行。 See example below.请参阅下面的示例。

Input:输入:

I like
I like apples
I like apples and oranges
more
more everyday
more everyday
more everyday the more
more everyday the more I play
I
I like

Output:输出:

I like apples and oranges
more everyday the more I play
I like

One way:单程:

$ perl -lne 'print $last unless /^\Q$last/; $last = $_;
             END { print $last }' input.txt
I like apples and oranges
more everyday the more I play
I like

这个sed过滤器读取下一行并删除前一行,只要它匹配:

sed ':0;N;s/^\(.*\)\n\1/\1/;t0;P;D'

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

相关问题 如何从匹配行之后删除文件中的所有行? - How do I delete all lines in a file starting from after a matching line? 如何用另一个文件中的内容替换一个文件中第XX行之后的所有行? - How do I replace all lines after line XX in one file with content from another file? 如何删除带有 sed 的文件的部分行? - How do I delete part of the lines of a file with sed? 使用sed删除文本文件中的所有前导/后续空格 - Use sed to delete all leading/following blank spaces in a text file 如何删除另一个文件中指定的所有行 - How to delete all lines specified in another file 如何将文件的行随机插入另一个文本文件? - How do I insert lines of a file randomly into another text file? 如何从bash中的历史记录中选择行的子集? - How do I select a subset of lines from the history in bash? 如何在 linux 的命令行上使用正则表达式过滤文本文件中以大写字母开头并以正 integer 结尾的行? - How do I filter lines in a text file that start with a capital letter and end with a positive integer with regex on the command line in linux? 如何从文本文件中的第一个空行开始删除所有行? - How to remove all lines from a text file starting at first empty line? 如何使用尾巴和头部在配置文件中的一行之前立即显示5行 - How to display 5 lines immediately before a line in a config file using tail and head
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM