Reputation: 119
F1.txt
tom1 a1 b1 c1
bob2 d2 e2 f2
result
F2.txt
a1 b1 c1 tom1
d2 e2 f2 bob2
Hello everyone,can anyone help me out with this problem. My job is to shift the first word of each line in the file to the last position in that line of the given file. It is as shown in F2.txt. Here is the code which I have tried, but I did not get the desired output.
use strict;
use warnings;
open FILE1, "<final.l";
open FILE2, ">>finala11.l";
my($line, @line);
while (<FILE1>) {
$line=$_;
chomp($line);
@line = split("\t"," ",$line);
push(@line, shift(@line));
print FILE2 @line,"\n";
}
close (FILE1);
close (FILE2);
The output which I am getting in this file is:
F3.txt
a1b1c1tom1
d2e2f2bob2
But the expected output is as shown in F2.txt. Can you help me out in finding the mistake in the code to get the desired result?
Upvotes: 0
Views: 64
Reputation: 119
use strict;
use warnings;
open FILE1, "<final.l";
open FILE2, ">>finala11.l";
my ($line, @line);
while () {
$line = $_;
chomp($line);
@line = split(" ", $line, 2); # <<
push(@line, "\t", shift(@line)); # <<
print FILE2 @line, "\n";
}
close(FILE1);
close(FILE2);
Thanks I have just corrected the line which has been highlighted and got the desired output
Upvotes: 0
Reputation: 37146
Combine autosplit with an array slice:
$ perl -F/\t/ -lane 'print join "\t", @F[1..$#F,0]' f1.txt > f2.txt
Upvotes: 0
Reputation: 1128
You may use join:
print FILE2 join("\t", @line),"\n";
or:
print FILE2 "@line\n";
Upvotes: 2