Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-13-2009, 01:27 PM   #1
Registered: Jun 2009
Posts: 35

Rep: Reputation: 15
Bash script sorting multple columns together


I have a file that contains anywhere from 2 to 6 columns of data, which I need to sort based on one of these columns. Te catch is that I need all of the lines to stay together through the sorting, ei, the 6th entry in all columns has to stay together even though I am sorting based only one one column.

For example:

1 4 8
4 2 9
2 3 7
8 8 1

If sorted in ascending order by second column would have to look like:

4 2 9
2 3 7
1 4 8
8 8 1

Now, I can do this in C, I know how, but I -really- don't want to because it is a ridicluous pain in the the ass to do this in a low level language. So if someone can suggest a simple bash script (maybe using awk) that could do this it would be greatly appreciated.

Old 08-13-2009, 01:35 PM   #2
LQ Newbie
Registered: Aug 2009
Posts: 21

Rep: Reputation: 16
sort can do that: sort -k2 yourfile
Old 08-13-2009, 01:59 PM   #3
Registered: Jun 2009
Posts: 35

Original Poster
Rep: Reputation: 15
Heh, just figured that one out. Thanks


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
Bash script for sorting and renaming multiple mp3 files by id3 tags simonloach Linux - General 8 02-16-2013 10:07 AM
Bash script for sorting out FSTAB & mount points ? Sparxz Linux - Newbie 1 05-30-2009 08:04 PM
bash scripting: sorting and using using multiple files in a script daberkow Linux - Newbie 8 05-28-2009 10:24 AM
sorting columns in bash twistadias Linux - Newbie 8 08-25-2008 01:22 AM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 12:27 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:13 PM.

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