LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to exchange two columns of a file (https://www.linuxquestions.org/questions/programming-9/how-to-exchange-two-columns-of-a-file-462858/)

jackk294 07-11-2006 01:38 AM

how to exchange two columns of a file
 
hello,

now i have met a problem as follows:

there is a file having two columns for examples:
Name Age
..... .....
...... .....
..... .....

what should i do to exchange these two columns and save the file like that:
Age Name
..... .......
..... .......
..... .......

Thanks a lot!

gilead 07-11-2006 02:02 AM

If you have a file called list containing:
Code:

Bruce 34
Jim  26
Carol 22

Then running the following works:
Code:

awk '{ print $2 " " $1 }' list
34 Bruce
26 Jim
22 Carol

Or does the data contain spaces, etc?

nadroj 07-11-2006 02:06 AM

another method is to use the 'cut' and 'paste' commands

'cut -f2 fileName' will cut the 'Age' column; the second field of your tab-delimited file 'fileName'.
'cut -f1 fileName' will cut the 'Name' column; the second field of your tab-delimited file 'fileName'.
you could save the output of these commands to a file, ie 'age' and 'name', then run 'paste age name' and it will print the original file, with the columns switched. again, just save the output of that paste command to a file and itll be saved... and delete the temoprary files created with the 2 cut commands.

this is messy but oh well.. just another method.

jackk294 07-11-2006 03:27 AM

Quote:

Originally Posted by gilead
If you have a file called list containing:
Code:

Bruce 34
Jim  26
Carol 22

Then running the following works:
Code:

awk '{ print $2 " " $1 }' list
34 Bruce
26 Jim
22 Carol

Or does the data contain spaces, etc?

Thank you very very much,
I will learn more about "awk".
that's powerful

gilead 07-11-2006 02:34 PM

Quote:

Originally Posted by jackk294
Thank you very very much,
I will learn more about "awk".
that's powerful

You're welcome :) If you're going to be doing a lot of text manipulation, I recommend O'Reilly's "sed & awk" (http://www.amazon.com/gp/product/156...lance&n=283155). I'd also recommend having a look at perl, but that's a personal bias of mine ;)

jackk294 07-11-2006 09:20 PM

also thanks a lot:)
Quote:

Originally Posted by nadroj
another method is to use the 'cut' and 'paste' commands

'cut -f2 fileName' will cut the 'Age' column; the second field of your tab-delimited file 'fileName'.
'cut -f1 fileName' will cut the 'Name' column; the second field of your tab-delimited file 'fileName'.
you could save the output of these commands to a file, ie 'age' and 'name', then run 'paste age name' and it will print the original file, with the columns switched. again, just save the output of that paste command to a file and itll be saved... and delete the temoprary files created with the 2 cut commands.

this is messy but oh well.. just another method.


nadroj 07-11-2006 09:27 PM

your very welcome jackk

jackk294 07-11-2006 09:48 PM

I am a beginner of linux and just now enter a company about linux.
I graduated from university only just , so i have more to learn and have a lot of things to ask, i will disturb you again, hehe.
Quote:

Originally Posted by nadroj
your very welcome jackk


sundialsvcs 07-11-2006 11:37 PM

'awk' is definitely a program to get to know.

Basically, it's a complete programmable tool based on the idea of pattern-recognition. So you can search for various patterns (any one of several), as well as other, more-sophisticated conditions. And you can then break-up the resulting strings pretty much any way you like.

One of the most useful things I've done with it is to extract output from a mainframe printout. The mainframe could spit out the "printout" to a file which we could then download. And AWK could do the rest, even though the printout was anything but regular. There was a commercial product called "cambio" which did much the same thing, with a fancy GUI.


All times are GMT -5. The time now is 06:55 AM.