Sorting of multi-column output
How to sort a command's output by a specific column? Let's say output of NETSTAT to sort by State column?
|
check out the 'cut' utility
if you wanted the 3rd column of netstat output you could execute netstat | cut -f3 jpbarto |
Maybe my question isn't accurate enough. I meant not change the output of a command but to sort it sophisticated, based on a pattern rather than on a column number.
|
so sorry... bout the closest I can get is 'netstat | sort +4' need to strip the header information first though... otherwise its really ugly.
|
:-) netstat was just as an example but i'm ok to stick on it. Here is an output I got:
tcp 0 0 127.0.0.1:1521 127.0.0.1:32774 ESTABLISHED tcp 0 0 10.10.0.95:139 10.10.0.91:3486 ESTABLISHED how to sort it on a port number of local address? |
if all you want is information on 127.0.0.1 I'd imagine you could (and sorry if I'm getting problem specific again) either specify in netstat only 127.0.0.1 or a more general solution would be to pipe netstat to grep to sort
netstat | grep 127\.0\.0\.1:[0-9]* | sort +3 |
Thanks for advice but netstat is not the point. lEt's take another example:
ls -all -1 brings output like: ... -rw-r--r-- 1 cvs cvs 11411 Jul 19 15:33 dirs -rw-rw-r-- 1 cvs cvs 397 Apr 10 10:38 files ... I wanna sort _all_ the lines by minues (two digits coming after columns). This is just an example born by the original question: how to sort output by pattern? |
I see... well in that case you got me stumped... as far as I know there's no hard and fast way to do that ... how do you feel about bash or perl scripting?
|
I'm not a guru in shell scripting at all:-) AFAIK awk could help and hoped to get help here.
|
All my reference books are at work... and to be honest I've never used awk (never had a need) but I'll try to help however I can.
|
Finally I found what I was looking for. So, to sort output of "ls -all" by minute (as number) here is a command:
ls -al | awk '{a=$8;sub(/.*:/,"",a);print a, $0}' | sort | awk '{sub(/[^ ]* /,"",$0);print}' (This example originally is posted by Chuck Demas) |
I think this can be done a lot smaller:
ls -al | sort +1n -t: But both your and my solution are flawed (when using ls -al as example)..... The time/date field in ls -al has more formats: YYYY or HH:MM We both use the : as field seperator, which is not going to work for the YYYY format. |
All times are GMT -5. The time now is 02:15 PM. |