LinuxQuestions.org
Review your favorite Linux distribution.
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 11-05-2003, 07:45 AM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
unix sort from the right side


Can the sort command work from the right side?

lastcomm has some fields which are not always output, so it's much easier if I could sort from the right hand side, however after reading the manpage it does'nt appear possible.

Is there an easy shell solution?

lastcomm | egrep -v '0.[0-9][0-9]' | sort +1nr

> sort --v
sort (textutils) 2.0.21

Regards
 
Old 11-05-2003, 08:20 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Is this what you want:

sort +5 +4 +0 <somefile>

This wil sort coloumn 6, then 5, then 1 (in that order)

+4 (and +0) will only be used if:

1) +5 has similar entries (sub sort)
2) there is no column 6

You could use -t to set a seperator (standard space/tab).

I'm not familiar with lastcomm, so I don't know what kind of output it generates.
 
Old 11-10-2003, 07:58 AM   #3
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
Nope this is'nt what I want because lastcomm has some fields that are not always output/displayed. Those fields are for

+ flags, as recorded by the system accounting routines:
S -- command executed by super-user
F -- command executed after a fork but without a following exec
C -- command run in PDP-11 compatibility mode (VAX only)
D -- command terminated with the generation of a core file
X -- command was terminated with the signal SIGTERM

Is it possible to sort from the right side using Unix?

Failing that, is it easy in perl?

Example output

[root@myhost]# lastcomm
myprog abc ?? 44.99 secs Mon Nov 10 14:39
perl S root ?? 2.64 secs Mon Nov 10 14:39
ld abc ?? 56.50 secs Mon Nov 10 14:37
gcc abc ?? 3.54 secs Mon Nov 10 14:37
gcc abc ?? 14.56 secs Mon Nov 10 14:36
gcc abc ?? 1.08 secs Mon Nov 10 14:36
perl S root stdin 2.40 secs Mon Nov 10 14:36

Regards
 
Old 11-10-2003, 05:22 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Changed your example output a bit. Timestamps/dates only, no layout stuff.
Looks like this
Code:
$ cat end2front.input

perl S root ?? 2.64 secs Mon Nov 10 14:38
gcc abc ?? 14.56 secs Mon Nov 10 14:36
myprog abc ?? 44.99 secs Mon Nov 9 14:39
perl S root stdin 2.40 secs Mon Nov 10 14:35
gcc abc ?? 3.54 secs Mon Nov 10 14:37
ld abc ?? 56.50 secs Mon Nov 9 14:35
gcc abc ?? 1.08 secs Mon Nov 10 14:34

$ cat end2front.input | awk -F"." 'BEGIN{OFS="."}{ print $2,$1 }' | sort +5 | awk -F"." 'BEGIN{OFS="."}{ print $2,$1 }'

gcc abc ?? 1.08 secs Mon Nov 10 14:34
ld abc ?? 56.50 secs Mon Nov 9 14:35
perl S root stdin 2.40 secs Mon Nov 10 14:35
gcc abc ?? 14.56 secs Mon Nov 10 14:36
gcc abc ?? 3.54 secs Mon Nov 10 14:37
perl S root ?? 2.64 secs Mon Nov 10 14:38
myprog abc ?? 44.99 secs Mon Nov 9 14:39
Sorted on the last field. Play with the sort options to make it the way you want.

Don't know if you see what it is the code is doing, so i'll explain just in case......

The dot (.) in the numbers before secs seem to be unique so I use that to split the line in two. The first field has the 'unpredictable' part, the second field has a layout that doesn't change and is needed for the sort.

I switch the fields. first the predictable field followed by the unpredictable field. I use a dot (.) as the ouput field (need that later on to switch the to fields back).

Now I can do a sort with predictable field(s).

After sorting we switch the 2 fields back to their original position and print the line.

Last edited by druuna; 11-10-2003 at 05:24 PM.
 
Old 11-11-2003, 02:52 AM   #5
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
I'd like to sort by process load :-)

The example is fine if the load is 0.xx, but if it's y.xx then these must be sorted and not just xx.

I'm sure this is easy, I just can't see it.

Thanks for your reply.

Regards
 
Old 11-11-2003, 06:05 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Mmmmmm.......... So you do not want to sort on the last field(s), but on the process load field. Why didn't you say so

Using the same input file as before:

$ cat end2front.input | sed 's/ \([0-9]\)/~\1/' | awk -F"~" 'BEGIN{OFS="~"}{ print $2,$1 }' | sort -n | awk -F"~" '{ print $2,$1 }'

gcc abc ?? 1.08 secs Mon Nov 10 14:34
perl S root stdin 2.40 secs Mon Nov 10 14:35
perl S root ?? 2.64 secs Mon Nov 10 14:38
gcc abc ?? 3.54 secs Mon Nov 10 14:37
gcc abc ?? 14.56 secs Mon Nov 10 14:36
myprog abc ?? 44.99 secs Mon Nov 9 14:39
ld abc ?? 56.50 secs Mon Nov 9 14:35
 
Old 11-11-2003, 08:34 AM   #7
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
Thank you! Works great! Very useful indeed!
 
  


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
Unix data sort question amytys Programming 2 07-01-2005 05:24 AM
gawk & sort commands in unix fanatic_ravi Linux - Software 0 01-25-2005 04:10 AM
C and C++ side-by-side examples Burgin Programming 5 09-17-2004 01:42 PM
help with client side NFS-firewall setup and server side NIS-firewall setup niverson Linux - Networking 3 02-02-2004 08:52 AM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM

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

All times are GMT -5. The time now is 03:42 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
Open Source Consulting | Domain Registration