Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Birbone (MIS) (OP)
28 Nov 07 11:41
The output is better when viewed in courier new 9 pt font.

I’m having trouble sorting absolutely by the second field.

The “cat $FILE|egrep -v "value1|value2|value3|value4|value5|value6"|cut -c1-80|sort +2 –if” command is utilized to generate the following system_id user_id pid and time output.

ctrbgone3             bgone             48794  08:56:44.78
ctrbgone3             bgone             57012  08:56:29.35
ctrsharedgui2         bgone             58890  09:48:52.91
ctrsharedgui3         bgone             59396  09:49:14.33
ctrsharedgui1         bgone             64580  09:46:32.52
ctrbgone3             bgone             68052  08:57:18.26
ctrvcthree2           vcthree           53410  10:01:19.00
ctrbricktwo1          bricktwo          43064  09:06:34.97

This output is correctly performing a group sort against the second column but not an absolute sort.  Thus all the bgone sessions are together but vcthree is appearing between bgone and bricktwo.

The above question is my primary need, but a second question is how do I get it to sort by column 2 and then column 1 so the output will look like this:

ctrbgone3             bgone             48794  08:56:44.78
ctrbgone3             bgone             57012  08:56:29.35
ctrbgone3             bgone             68052  08:57:18.26
ctrsharedgui1         bgone             64580  09:46:32.52
ctrsharedgui2         bgone             58890  09:48:52.91
ctrsharedgui3         bgone             59396  09:49:14.33
ctrbricktwo1          bricktwo          43064  09:06:34.97
ctrvcthree2           vcthree           53410  10:01:19.00

-B     looking around

columb (IS/IT--Management)
28 Nov 07 11:53
Try something along the lines of

CODE

egrep -v "value[1-6]" $FILE | cut -c1-80 | sort  -k2,3 -k1,2

Ceci n'est pas une signature
Columb Healy

columb (IS/IT--Management)
28 Nov 07 12:05
I was maybe a bit terse. Here's a fuller explanation

CODE

egrep -v "value[1-6]" $FILE
You almost never need to use cat and it's good scripting style to remove it. similarly with egrep we can use a regexp to shorten the expression.

CODE

cut -c1-80
No explanation neccessary

CODE

sort  -k2,3 -k1,2
The -k n,m flag says use as a key the columns n up to, but not including m, So in this case, use column 2 up to 3 and column 1 up to 2.

This seems to sort (no pun intended) out your other problem. I'm not sure why. I've dropped the -i and -f flags - you don't need them in this case.

Ceci n'est pas une signature
Columb Healy

Birbone (MIS) (OP)
28 Nov 07 14:23
Yea, I had tried that and several other options earlier, but retried it again without the cat and -if or +2 options. Although it still displayed incorrectly, it gave me some other ideas to try that can refine my explaination of what is occuring.  

The "sort -k3,#" has to be used to correctly sort on the correct field without the +2 option.  The # here represents both the same field number as well as other field numbers (i.e. "-k 3,3" and "-k 3,4" etc). This was also tested with and without the -df options, as well as with an additional -k1,# option.

ctrbgone3             bgone             48794  08:56:44.78
ctrbgone3             bgone             57012  08:56:29.35
ctrsharedgui2         bgone             58890  09:48:52.91
ctrsharedgui3         bgone             59396  09:49:14.33
ctrsharedgui1         bgone             64580  09:46:32.52
ctrbgone3             bgone             68052  08:57:18.26
ctrvcthree2           vcthree           53410  10:01:19.00
ctrbricktwo1          bricktwo          43064  09:06:34.97

Basically there are two observations.  

First, the sort -k 3,# begins correctly but is looking at it from that point to the end of the line as the sort "field".  Thus the "bgone" ids are actually sorted by user_id including the pid and time stamp, so the additional -k 1,# option never comes into play.

Secondly, the sort is not sorting strickly alphabetically even with the -df option, but is sorting alphabetically based on identical length values.  Thus all five character names are sorted alphabetically followed by all six character names, then seven, eight, nine, etc.
  

-B     looking around

Birbone (MIS) (OP)
29 Nov 07 10:22
Considering the information in my last post, I still need help on this.

-B     looking around

Mike042 (MIS)
30 Nov 07 8:38
Something like:

   ...... | sort -k2f -k1f

(sort using ASCII character values but ignoring 'case' for column 2 then column 1 where 'white-space' is the column separator)

has work for me in a similar situation (on Solaris in ksh).


I hope that helps.

Mike

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close