LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-11-2006, 01:38 AM   #1
jackk294
LQ Newbie
 
Registered: Sep 2004
Posts: 7

Rep: Reputation: 0
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!
 
Old 07-11-2006, 02:02 AM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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?
 
Old 07-11-2006, 02:06 AM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
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.
 
Old 07-11-2006, 03:27 AM   #4
jackk294
LQ Newbie
 
Registered: Sep 2004
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 07-11-2006, 02:34 PM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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
 
Old 07-11-2006, 09:20 PM   #6
jackk294
LQ Newbie
 
Registered: Sep 2004
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 07-11-2006, 09:27 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
your very welcome jackk
 
Old 07-11-2006, 09:48 PM   #8
jackk294
LQ Newbie
 
Registered: Sep 2004
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 07-11-2006, 11:37 PM   #9
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,377

Rep: Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108
'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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading in data in columns from a file (C++) Nylex Programming 4 03-08-2006 05:55 PM
Need Help With Columns Post Modern Programming 2 02-01-2006 11:18 AM
Is there a command to merge two files as two columns of one file? davee Linux - General 2 07-19-2005 10:52 AM
linus/win file exchange andreamazza Red Hat 1 09-22-2004 01:46 PM
How can I see the other 10 columns sakulagi Linux - Software 1 06-16-2003 09:45 PM


All times are GMT -5. The time now is 12:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration