根据模式删除txt文件中的行

时间:2018-09-16 08:31:06

标签: regex awk sed grep

我有一个txt文件,其内容类似于

    value1 has output a123
    a123 has output 90
    a123 has output 5000
    a123 has output 400
    value2 has output 100
    value3 has output a133
    a133 has output 50
    a133 has output 90
    value4 has output a223
    a223 has output 100

我要删除重复两次的行(出现在前一行的最后一个单词中的行),例如-

 a123 has output 90
 a123 has output 5000
 a123 has output 400
 a133 has output 50
 a133 has output 90
 a223 has output 100

并保留这些-

value1 has output a123
value2 has output 100
value3 has output a133 
value4 has output a223

3 个答案:

答案 0 :(得分:2)

以下awk命令将执行:

awk '$1!=l{print;l=$NF}' your_text_file

答案 1 :(得分:1)

考虑到您要从以字符串value开始的行中获取值,那么下面的操作也可能对您有帮助。

awk '/^value/{value=$NF} $1!=value'  Input_file

答案 2 :(得分:1)

这可能对您有用(GNU sed):

sed -r ':a;N;s/((\S+)\s*)\n\s*\2\s.*/\1/;ta;P;D' file

将下一行与上一行进行比较,如果下一行的最后一个单词与上一行的最后一个单词相同,请删除下一行并重复。