LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sorting of multi-column output (https://www.linuxquestions.org/questions/linux-newbie-8/sorting-of-multi-column-output-90865/)

rytrom 09-09-2003 09:20 AM

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?

jpbarto 09-09-2003 09:35 AM

check out the 'cut' utility

if you wanted the 3rd column of netstat output you could execute

netstat | cut -f3

jpbarto

rytrom 09-09-2003 02:34 PM

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.

jpbarto 09-09-2003 03:09 PM

so sorry... bout the closest I can get is 'netstat | sort +4' need to strip the header information first though... otherwise its really ugly.

rytrom 09-09-2003 03:22 PM

:-) 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?

jpbarto 09-09-2003 03:40 PM

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

rytrom 09-09-2003 03:46 PM

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?

jpbarto 09-09-2003 04:05 PM

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?

rytrom 09-09-2003 04:13 PM

I'm not a guru in shell scripting at all:-) AFAIK awk could help and hoped to get help here.

jpbarto 09-09-2003 09:26 PM

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.

rytrom 09-15-2003 10:58 AM

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)

druuna 09-15-2003 11:31 AM

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.