LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-26-2016, 07:06 PM   #1
Corrado
Member
 
Registered: Aug 2004
Location: Washington
Distribution: RHEL
Posts: 174

Rep: Reputation: 16
Help with sort command


I've been struggling to sort a file with multiple columns. Maybe somebody could help me out?

What I am trying to do is sort the times in the last column numerically in reverse. The delimiter being spaces between columns is not always equal in number.

Does anyone know a good website with explains and gives examples of various commands? The man pages are sometimes Greek to me.

Quote:
DATE & TIME HOST RELEASE LS TIME
06-26 16:12:02 349 rhel-6.6 0:03.00
06-26 16:12:08 351 rhel-6.6 0:05.65
06-26 16:12:15 352 rhel-6.6 0:06.09
06-26 16:12:22 353 rhel-6.7 0:06.38
06-26 16:12:29 355 rhel-6.6 0:05.33
06-26 16:12:36 356 rhel-6.6 0:02.60
06-26 16:12:39 357 rhel-6.6 0:06.35
06-26 16:12:46 358 rhel-6.6 0:03.21
 
Old 06-26-2016, 09:11 PM   #2
tayfie
LQ Newbie
 
Registered: Jun 2016
Posts: 5

Rep: Reputation: 0
If you don't care about preserving the spaces, it makes everything easier to get rid of the extras. Otherwise, you are looking at some scripting in the language of your choice.

Code:
tr -s ' ' < file.txt >newfile.txt
The two important commands for dealing with column manipulation are cut and paste.

Code:
cut -d ' ' -f5 newfile.txt | paste - newfile.txt | sort -r | cut -d ' ' -f 2- >sorted.txt
sorted.txt contains the result you want.

What this does is paste the desired column on the front, sort that, and cut it off again.

As for web examples, I'm not sure how advanced you had in mind. Mostly I just google the command name I want to know about.

Last edited by tayfie; 06-26-2016 at 09:14 PM.
 
Old 06-26-2016, 09:16 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I can't believe a search on "linux sort examples" didn't turn up a bunch of useful sites.

Sort doesn't care about the number of consecutive separators - it senses the transition from whitespace to non. Sort fields are called "keys", so you use -k for that, and -r to reverse the sort. All in the manpage, although as is normally the case "info sort" is more expansive.
Header/footer lines are usually a problem, but you might get lucky.

Edit: ... ahh, now I see the problem. Check out the "-V" parameter. Very useful on occasions.

Last edited by syg00; 06-26-2016 at 09:23 PM.
 
Old 06-27-2016, 07:25 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by Corrado View Post
The delimiter being spaces between columns is not always equal in number.
Can you post some examples of the rows with different spacings? With "sort" the amount of whitespace is not as important as if there are empty columns.

Otherwise, syg00 suggests there is -r and -k. The manual page for "sort" is quite hard to follow, but with the data above you would use -rk 5,5 because the colons throw off any use of the numeric option (-n) and the numbers are in order.
 
Old 06-27-2016, 07:53 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Yes, it would be very helpful if you could post both a (of course, "sanitized") version of the actual data, showing any "different in number" differences that you speak of.

Then, show us the actual sort command that you are using, and carefully tell us, in your own words, how you intend for this file to be correctly sorted.
 
Old 06-27-2016, 09:43 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I don't get it... as long as the actual columns are not different, this seems to work (or?!?):
Code:
echo "06-26 16:12:02 349 rhel-6.6 0:03.00
06-26 16:12:08 351 rhel-6.6 0:05.65
06-26 16:12:15 352 rhel-6.6 0:06.09
06-26 16:12:22 353 rhel-6.7 0:06.38
06-26 16:12:29 355 rhel-6.6 0:05.33
06-26 16:12:36 356 rhel-6.6 0:02.60
06-26 16:12:39 357 rhel-6.6 0:06.35
06-26 16:12:46 358 rhel-6.6 0:03.21" | sort -k 5 -r
06-26 16:12:22 353 rhel-6.7 0:06.38
06-26 16:12:39 357 rhel-6.6 0:06.35
06-26 16:12:15 352 rhel-6.6 0:06.09
06-26 16:12:08 351 rhel-6.6 0:05.65
06-26 16:12:29 355 rhel-6.6 0:05.33
06-26 16:12:46 358 rhel-6.6 0:03.21
06-26 16:12:02 349 rhel-6.6 0:03.00
06-26 16:12:36 356 rhel-6.6 0:02.60
Then again, I am probably missing what the OP really wants here.

Best regards,
HMW
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using the sort command castor0troy Linux - Newbie 6 04-23-2013 10:01 AM
[SOLVED] sort command help anu_1 Linux - Newbie 4 01-29-2010 04:34 AM
Sort command arya6000 Linux - Newbie 2 11-27-2007 07:50 PM
Sort Command saravanan1979 Programming 1 10-03-2004 11:36 AM
Using the Sort command in vi timnphx Programming 2 04-06-2001 11:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration