LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices



Reply
 
Search this Thread
Old 09-09-2003, 10:20 AM   #1
rytrom
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Rep: Reputation: 15
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?
 
Old 09-09-2003, 10:35 AM   #2
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
check out the 'cut' utility

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

netstat | cut -f3

jpbarto
 
Old 09-09-2003, 03:34 PM   #3
rytrom
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
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.
 
Old 09-09-2003, 04:09 PM   #4
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
so sorry... bout the closest I can get is 'netstat | sort +4' need to strip the header information first though... otherwise its really ugly.
 
Old 09-09-2003, 04:22 PM   #5
rytrom
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
:-) 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?
 
Old 09-09-2003, 04:40 PM   #6
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
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
 
Old 09-09-2003, 04:46 PM   #7
rytrom
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
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?
 
Old 09-09-2003, 05:05 PM   #8
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
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?
 
Old 09-09-2003, 05:13 PM   #9
rytrom
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
I'm not a guru in shell scripting at all:-) AFAIK awk could help and hoped to get help here.
 
Old 09-09-2003, 10:26 PM   #10
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
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.
 
Old 09-15-2003, 11:58 AM   #11
rytrom
LQ Newbie
 
Registered: Jul 2003
Posts: 27

Original Poster
Rep: Reputation: 15
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)
 
Old 09-15-2003, 12:31 PM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
  


Reply


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
Sorting data in output file lluciano Linux - General 2 08-19-2005 11:37 AM
How do you change card# of multi multi-media cards. Nichole_knc Slackware 3 12-01-2004 01:49 PM
ssh lock up on multi line output Incanus Linux - Networking 0 07-20-2004 09:47 PM
GRUB, Multi Linux/Multi Disk Boot gtnorton Linux - Software 1 03-16-2003 04:48 AM
grepping for multi-line output born4linux Programming 1 03-07-2003 03:26 AM


All times are GMT -5. The time now is 10:12 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration