perl删除连续的重复行

chi*_*und 3 regex perl delete-row

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

**test.txt**
car
speed is good
bike 
slower than car
plane
super fast
super fast
bullet train 
super fast
Run Code Online (Sandbox Code Playgroud)

这将删除除第一次出现之外的所有重复行.

perl -ne 'print unless $a{$_}++'
Run Code Online (Sandbox Code Playgroud)

但我想要的是输出

    **test.txt**
    car
    speed is good
    bike 
    slower than car
    plane
    super fast
    bullet train 
    super fast
Run Code Online (Sandbox Code Playgroud)

我试过这个oneliner,但这并没有做什么,只是打印输入.

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

这该怎么做???

Ste*_*eve 10

你为什么不用uniq

uniq file.txt
Run Code Online (Sandbox Code Playgroud)

结果:

car
speed is good
bike 
slower than car
plane
super fast
bullet train 
super fast
Run Code Online (Sandbox Code Playgroud)

您也可以这样做awk:

awk 'line != $0; { line = $0 }' file.txt
Run Code Online (Sandbox Code Playgroud)


小智 5

$ perl -ne 'print $_ unless $_ eq $prev; $prev = $_'
Run Code Online (Sandbox Code Playgroud)


eps*_*lon 5

尝试:

perl -ne 'print unless (defined($prev) && ($_ eq $prev)); $prev=$_'
Run Code Online (Sandbox Code Playgroud)