How to exchange two columns in a table?

Tim Chase blinux.list at thechases.com
Mon Jan 8 18:42:11 UTC 2007


> You can use cut for this:
> 
> cut -d "	" -f 1-3,5,4,6-8
> 
> (the character between the two " is a tab, which you can type by
> pressing ^V then the tab.  Here columns 3 and 4 are exchanged.

It would be really nice if "cut" did this.  However, cut doesn't 
emit the fields in the requested order, but rather (from the man 
page) "The  order of ... fields in the output will be identical 
to those in the input".  I've been stung by this several times, 
only to sulk away nursing my wounds with some awk or python each 
time.  At least "cut" doesn't behave the way you describe on the 
several boxes I have at hand (several Linux boxes ATM...don't 
know how it would behave on a BSD, Mac OX X, or Solaris box, but 
I could test it on an OpenBSD or Mac box when I get a chance).

So, if it works for you, count yourself lucky...but prepared for 
it to return a surprising result.

Additionally, if you want to be lazy, the delimiter for "cut" 
defaults to a tab, so you don't have to specify it unless you use 
a different delimiter such as a colon.

-tim







More information about the Blinux-list mailing list