Swap two columns

J

Jürgen Exner

How can I swap two columns in a file with perl?

Your Question is Asked Frequently, please see "perldoc -q change":

How do I change one line in a file[...]?

jue
 
R

robic0

How can I swap two columns in a file with perl?
I'm not %100 positive on this but:

If its a square file, open for read and write '+<',
- seek to a position, read a line
- swap data in memory
- seek to same position, write line.

Usually its not opend this way. The alternative is
to open a new file, then for the last step write to
the new file (won't need the seek then).

See the docs
 
J

jpeg

How can I swap two columns in a file with perl?

split and array slices work well. Here's an example for a tab-delimited
file.

#!/usr/bin/perl
use strict;

my @cols;
while (<DATA>) {
chomp;
@cols = split(/\t/, $_);
print ($cols[1] . "\t" . $cols[0] . "\n");
}
__DATA__
1 2
3 4
5 6
7 8
9 10
 
J

Josef Moellers

jpeg said:
How can I swap two columns in a file with perl?


split and array slices work well. Here's an example for a tab-delimited
file.

#!/usr/bin/perl
use strict;

my @cols;
while (<DATA>) {
chomp;
@cols = split(/\t/, $_);
print ($cols[1] . "\t" . $cols[0] . "\n");
}
__DATA__
1 2
3 4
5 6
7 8
9 10

This only works if there is only a single tab in the file.
In general, however, this works if you change the print to the following:

{ my $tmp = $cols[1]; $cols[1] = $cols[0]; $cols[0] = $tmp; }
print join("\t", @cols), "\n";

Most likely, there is some clever way to replace the first line by a
single statement or do without $tmp ...

Josef
 
A

Anno Siegel

Josef Moellers said:
jpeg said:
How can I swap two columns in a file with perl?


split and array slices work well. Here's an example for a tab-delimited
file.

#!/usr/bin/perl
use strict;

my @cols;
while (<DATA>) {
chomp;
@cols = split(/\t/, $_);
print ($cols[1] . "\t" . $cols[0] . "\n");
}
__DATA__
1 2
3 4
5 6
7 8
9 10

This only works if there is only a single tab in the file.
In general, however, this works if you change the print to the following:

{ my $tmp = $cols[1]; $cols[1] = $cols[0]; $cols[0] = $tmp; }
print join("\t", @cols), "\n";

Most likely, there is some clever way to replace the first line by a
single statement or do without $tmp ...

Certainly:

@cols[ 0, 1] = @cols[ 1, 0];

Anno
 
J

Josef Moellers

Anno said:
Josef Moellers said:
jpeg said:
On Thu, 06 Apr 2006 17:27:48 -0700, rzaleski wrote:



How can I swap two columns in a file with perl?


split and array slices work well. Here's an example for a tab-delimited
file.

#!/usr/bin/perl
use strict;

my @cols;
while (<DATA>) {
chomp;
@cols = split(/\t/, $_);
print ($cols[1] . "\t" . $cols[0] . "\n");
}
__DATA__
1 2
3 4
5 6
7 8
9 10

This only works if there is only a single tab in the file.
In general, however, this works if you change the print to the following:

{ my $tmp = $cols[1]; $cols[1] = $cols[0]; $cols[0] = $tmp; }
print join("\t", @cols), "\n";

Most likely, there is some clever way to replace the first line by a
single statement or do without $tmp ...


Certainly:

@cols[ 0, 1] = @cols[ 1, 0];

Hey, two wishes in one, can I have the toy too ;-)?

Thanks,

Josef
 
D

Dr.Ruud

Josef Moellers schreef:
print join("\t", @cols), "\n";

Alternative:

{ local ($", $\) = ("\t", "\n"); print "@cols" }

Test:

perl -e '@cols=(1,2,3); @cols[0,1]=@cols[1,0];
($",$\)=("\t","\n"); print "@cols"'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top