Re: How to exchange two columns in a table?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If there is only one tab between fields you can use PERL like so:

$ cat file | swap.pl > file2



Where swap.pl contains:

#/usr/bin/perl

while(<STDIN>)
{
chomp $_;
($col1,$col2) = split(/\t/,$_);
print "$col2\t$col1\n";
}
# end of swap.pl file


The above assumes  that swap.pl is executable. chmod 755 it  after editing. It also
assumes there are only two columns in the file. If there are more, e.g. 4 you will
ned to adjust the code accordingly, such as
($col1,$col2,$col3,$col4) = split(/\t/,$_);
and
print "$col2\t$col1\t$col3\t$col4\n";

or whichever columns you want to swap.

Rudy Vener
Pizza galaxy
http://www.pizzagalaxy.com



On Sat, Jan 06, 2007 at 11:14:40PM +0800, coscell@xxxxxxxxxxxxxx wrote:
> Thank you! It's just a tab-delimited text file.
> 
> On Sat, 6 Jan 2007, Tim Chase wrote:
> 
> >I think you'll have to provide a few more details:
> >
> >1) where is the table?  In a spreadsheet?  In a database?  In a text-file?
> >
> >2) If it's a spreadsheet or database, what flavor? (Gnumeric? Siag?  SC? 
> >Oleo?  OpenOffice.org?  KOffice?  MySQL?  PostgreSQL?
> > SQLite? etc)
> >
> >3) If it's just a text file, how are columns determined?  Is it a CSV 
> >file? (and if so, how is character escaping/quoting done?) Is it a 
> >tab-delimited file?  Are the columns positional? (i.e. columns 1 through 
> >10 are the first field, 11 through 15 are the next, etc)
> >
> >With the extra details, it should be pretty easy to find a solution.
> >
> >-tim
> >
> >
> >
> >_______________________________________________
> >Blinux-list mailing list
> >Blinux-list@xxxxxxxxxx
> >https://www.redhat.com/mailman/listinfo/blinux-list
> >
> 
> _______________________________________________
> Blinux-list mailing list
> Blinux-list@xxxxxxxxxx
> https://www.redhat.com/mailman/listinfo/blinux-list

_______________________________________________
Blinux-list mailing list
Blinux-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/blinux-list

[Index of Archives]     [Linux Speakup]     [Fedora]     [Linux Kernel]     [Yosemite News]     [Big List of Linux Books]