perl删除连续的重复行 [英] perl to delete consecutive duplicate lines

查看:101
本文介绍了perl删除连续的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除连续重复的行。即例如

I want to delete the consecutive duplicate lines. i.e. for example

**test.txt**
car
speed is good
bike 
slower than car
plane
super fast
super fast
bullet train 
super fast

这会删除所有重复的行,但第一次出现除外。

This removes all the duplicate lines except the 1st occurance.

perl -ne 'print unless $a{$_}++'

但我想要输出成为

    **test.txt**
    car
    speed is good
    bike 
    slower than car
    plane
    super fast
    bullet train 
    super fast

我尝试了此oneliner,但这只是打印输入内容而没有做任何事情。

I tried this oneliner but this doesnt do anything but just prints the input.

perl -00 -F'<\w+>|</\w+>' -i.bak -lane 'foreach(@F){if ($_=~/\w+/ && ($a ne $_)){print "$_";$a=$_;}}'

操作方法?

推荐答案

尝试:

perl -ne 'print unless (defined($prev) && ($_ eq $prev)); $prev=$_'

这篇关于perl删除连续的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆