Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort by column

Status
Not open for further replies.

Birbone

MIS
Dec 20, 2000
141
US
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 :cool:
 
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
 
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
 
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 :cool:
 
Considering the information in my last post, I still need help on this.

-B :cool:
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top