1831185 Members
2836 Online
110021 Solutions
New Discussion

sort problem

 
SOLVED
Go to solution
j773303
Super Advisor

sort problem

vi test.txt
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5

When "sort a > b", the b would show
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3

But I want to get the result like below:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3


Because it is only search column1, and "1 aaa 7" is in the first line, but not "1 aaa 4" . Does sort command can do that ? Thanks.
Hero
14 REPLIES 14
Steve Steel
Honored Contributor

Re: sort problem

Hi

not what you show because that is not sorted but read the man

sort +2 -r /tmp/test.txt
1 aaa 7
1 aaa 5
1 aaa 4
2 bbb 3

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
j773303
Super Advisor

Re: sort problem

I want the result as below:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3

Becuase I select column 1 as the sort key.
See the source file,
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5

Since
1 aaa 7 is the first line
1 aaa 4 is the third line
1 aaa 5 is the last line

So, I want sort as below results:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3

But sort will get the below results:
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3

It looks line it also sort the column 2 and column3. From my point, I just want sort column1.

Hero
Bharat Katkar
Honored Contributor

Re: sort problem

# more file1
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5

The output.
# more file1 | sort +1
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3

This what i get but not 745 in the last col.

Another way to add to it is:
# more file1 | sort +1 | sort +3 -r
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3

This is what i get but again no 745 in the last col.

That's all.
Regards,
You need to know a lot to actually know how little you know
Dietmar Konermann
Honored Contributor

Re: sort problem

Looks like sort(1) is not able to do what you want... according to the man page:

---
Lines that otherwise compare equal are ordered with all bytes significant. If all the specified keys compare equal, the entire record is used as the final key.
---

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Muthukumar_5
Honored Contributor

Re: sort problem

Hai,

sort will try to check on key value first, If it is containing same vaules,it will go to next and so on.

1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5

cat test.file | sort -k 1

There is no unique field in first key,so it is coming to second. Situation at 2 column is same. At last it is coming to 3rd one.

Result will be as like that.

1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 3

It will give as,
1 aaa 3
1 aaa 4
1 aaa 7
2 bbb 3

It is sorting every column if there is no unique field to validate this,

Change the input file as,
4 2 bbb 3
5 1 aaa 4
3 1 aaa 5
1 1 aaa 7

cat test.file | sort -k 1

1 1 aaa 7
3 1 aaa 5
4 2 bbb 3
5 1 aaa 4


It is sorting only on first field only.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Dietmar Konermann
Honored Contributor

Re: sort problem

Ugly workaround... prepend a line number, sort with line number a 2nd key and finally strip it off again. :)

awk '{print NR, $0}' | sort -k 2,2 -k 1n,1n | cut -d ' ' -f 2-
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Victor BERRIDGE
Honored Contributor

Re: sort problem

Or:
tant # more a
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
tant # head -n1 a>c && cat a|grep -v "`head -n1 a`"|sort>>c
tant # more c
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3

All the best
Victor
RAC_1
Honored Contributor

Re: sort problem

cat file|sort -rnk3

Gives, what you want

Anil
There is no substitute to HARDWORK
Chris Wilshaw
Honored Contributor
Solution

Re: sort problem

As you've seen, sort can't really do what you want, but there's always a solution

for num in 1 2
do
grep "^$num " test.txt >> test2.txt
done

(note the space following $num in the quotes)
test2.txt then contains

1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
Chris Wilshaw
Honored Contributor

Re: sort problem

I'd also suggest you take a look at your point assignments;

From your profile;

http://forums1.itrc.hp.com/service/forums/publicProfile.do?userId=CA875590&forumId=1

You have assigned points to 426 of 1042 responses to your questions.

That relates to a lot of assistance that you've received that you've not acknowledged.
harry d brown jr
Honored Contributor

Re: sort problem


You want to take a file like this
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5

and put it into the form of this:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3

Sorry, but that's not called sorting!

It appears you are trying to move the records down based upon the first value. Thus I suggest you use something like awk:

cat test.txt | awk 'BEGIN {system("rm newcraze.txt")} {print $0 > "craze"$1".tmp
";files[$1]="Y"} END {for(var=0;var<9;var++) {if (files[var] == "Y") {system("ca
t craze"var".tmp >>newcraze.txt");system("rm craze"var".tmp")}}}'

live free or die
harry
Live Free or Die
Hein van den Heuvel
Honored Contributor

Re: sort problem


Actually... on some systems this is called sorting, specifically 'stable' sorting.
IMHO, if one specifies explicit keys then a sort tool has no business to add in gratuitous extra keys (the whole record if equal). It could (IMHO) return records in random orders, or... when so requested in a STABLE order based on the input file order.
OpenVMS SORT (and SYNCSORT?) work that way. (See below).
Still, that does not matter because hpux sort is documented NOT to work that way.

So here is a 'one-liner' workaround...

awk '{printf ("%06d %s\n",NR,$0)}' x | sort -k 2,2 -k 3,3 -k 1,1 xx | cut -b 7-


hth,
Hein.

OpenVMS example...

$ typ tmp.tmp
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
$ sort/key=(pos:1,siz:5)/stable tmp.tmp sys$output:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
Hein van den Heuvel
Honored Contributor

Re: sort problem

Oops, I missed Dietmars earlier reply with the same/similar workaround of prepending a line number. (except that I opted for characters versus fields in the cut)

Hein.
Rodney Hills
Honored Contributor

Re: sort problem

If you want the first 2 fields in ascending order and the third field descending, then what about the following-

sort -k1,2 -k3r testfile

HTH

-- Rod Hills
There be dragons...