LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-30-2008, 08:09 AM   #1
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Using awk to switch columns


I have a file of the following form:

001 002
001 003
001 004
001 003
002 001
002 004
002 004
002 005
... ...

Which has already been sorted once. However, the column position is trivial as in 001 002 is the same as 002 001, so what I want to do is go down each line and if the number in the second column is smaller than the number in the first column, I want them to switch positions. Is this possible?
 
Old 10-30-2008, 08:25 AM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Sure:

Code:
awk '{ if($2 < $1){print $2" "$1} else {print $1" "$2} }' testing.txt
Code:
001 002
001 003
001 004
001 003
001 002
002 004
002 004
002 005
 
Old 10-30-2008, 08:52 AM   #3
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
TeXMeX,

You beat me to the punch, I was just about to post the EXACT same code, but I had a semicolon before the else -- which I thought you needed? Does it not matter?
 
Old 10-30-2008, 09:50 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
No, there should not be a semicolon before the else. It's just a C-style if-else statement. Remember to use the '>' to output the result to a file.
 
  


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
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
awk print correct lines when certain columns are blank schneidz Programming 11 04-04-2008 04:06 PM
Moving columns with sed or awk? btm Linux - Newbie 4 09-27-2007 02:03 PM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM
tail with -f switch | awk yongitz Programming 8 12-19-2006 03:50 AM


All times are GMT -5. The time now is 08:05 PM.

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