tags:

views:

171

answers:

2

Hi,

I am having a file in the following format

Column1    Column2
str1       1
str2       2
str3       3

I want the columns to be rearranged. I tried below command

cut -f2,1 file.txt

The command doesn't reorder the columns. Any idea why its not working?

Thank you.

+5  A: 

For the cut(1) man page:

   Use one, and only one of -b, -c or -f.  Each LIST is  made  up  of  one
   range,  or  many ranges separated by commas.  Selected input is written
   in the same order that it is read, and is written exactly  once.

It reaches field 1 first, so that is printed, followed by field 2.

Use awk instead:

awk '{ print $2 " " $1}' file.txt
Ignacio Vazquez-Abrams
A: 

using just the shell,

while read -r col1 col2
do
  echo $col2 $col1
done <"file"
ghostdog74