Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.