如何去除开始用相同的字连续行的第二行? [英] How to remove the second line of consecutive lines starting with the same word?
问题描述
我有交换行以'标题'和'数据',但有时也有以'TITLE重复行的文本文件:
I have a text file with interchanging lines starting with 'TITLE' and 'DATA' but sometimes there are duplicate lines starting with 'TITLE':
TITLE什么结果
数据的一些数据结果
TITLE别的结果
数据的其他数据结果
TITLE一些结果
TITLE额外的信息结果
数据更多的数据。
TITLE something
DATA some data
TITLE something else
DATA some other data
TITLE some more
TITLE extra info
DATA some more data
我想能够检测以TITLE重复的线路,只保留每对的第一行。结果
我想通了,捕捉这些常规的前pression是 ^ TITLE。* \\ n ^ TITLE。* \\ n
现在我想将其纳入一个本单行 perl的
/ 庆典
/ SED
/ AWK
命令,将删除第二行和输出文件的休息,但我想不出这一点。
I'd like to be able to detect the duplicate lines starting with 'TITLE' and keep only the first line of each such pair.
I figured out that the regular expression for capturing these is ^TITLE.*\n^TITLE.*\n
now I'd like to incorporate this into a one-liner perl
/bash
/sed
/awk
command that would remove the second line and output the rest of the file, but I couldn't figure this out.
推荐答案
Perl的解决方案:
Perl solution:
perl -ne 'print unless $t and /^TITLE/; $t = /^TITLE/'
据记住previous线是否是在 $ T
变量的标题。
这篇关于如何去除开始用相同的字连续行的第二行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!