我想删除连续的重复行.即,例如
**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)
这该怎么做???
我有2个文件
**a.txt**
cars
bikes
bus
vehicle
atv
**b.txt**
hawk
hero
atv
bus
***result.txt***
cars
bikes
vehicle
hawk
hero
Run Code Online (Sandbox Code Playgroud)
我想打印2个文件之间的差异.现在我尝试了一个代码,但它以随机的方式给了我差异; 我希望它以适当的顺序方式显示它.任何人都可以帮助我.
use strict;
my %results = ();
open FILE1, "<a.txt"
or die "Could not open file: $! \n";
while (my $line = <FILE1>) {
$results{$line}=1;
}
close FILE1;
open FILE2, "<b.txt"
or die "Could not open file: $! \n";
while (my $line = <FILE2>) {
$results{$line}++;
}
close FILE2;
open OUTFILE, ">>result.txt"
or die "Cannot open $outfile for writing \n";
foreach …Run Code Online (Sandbox Code Playgroud) F1.txt
bob
tom
harry
Run Code Online (Sandbox Code Playgroud)
F2.txt
bob
a=1 b=2 c=3
bob
d=4 e=5 f=6
tom
a1=34 b1=32 c1=3443
tom
a2=534 b2=732 c2=673443
Run Code Online (Sandbox Code Playgroud)
结果:
A1.txt
bob
a=1 b=2 c=3
bob
d=4 e=5 f=6
Run Code Online (Sandbox Code Playgroud)
A2.txt
tom
a1=34 b1=32 c1=3443
tom
a2=534 b2=732 c2=673443
Run Code Online (Sandbox Code Playgroud)
我是PERL的新手你可以帮助我解决我的问题.现在我已经提到了两个文件,即F1.txt和F2.txt,我的工作是在F2.txt中搜索F1.txt的任何元素并打印相应的行以及下一行.如果找到一个元素,那么结果必须保存在一个新文件中,我给出了示例A1.txt,它存储了关于bob的所有信息,类似地,A2.txt它存储了关于Tom的所有信息.直到现在我已经尝试过这段代码,但效率不高,
use strict;
use warnings;
my $line1;
my $line2;
my $fh;
my $fh1;
my $counter;
open $fh, "<", "F1.txt" or die $!;
open $fh1, "<", "F2.txt" or die $!;
my @b = <$fh>;
my @a = <$fh1>;
for (@b)
{ …Run Code Online (Sandbox Code Playgroud)