- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- numeric sort
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 06:46 AM
05-22-2002 06:46 AM
aaaaa+0000001+
bbbbb+0000002+
ccccc+0000003+
ddddd+0000004+
eeeee-0000003-
fffff-0000002-
ggggg-0000001-
ggggg+0000001+
ggggg+0000001+
ggggg-0000004-
the command
sort -n -k 1.6,1.13
with the previous file produces:
ggggg-0000004-
eeeee-0000003-
fffff-0000002-
ggggg-0000001-
aaaaa+0000001+
bbbbb+0000002+
ccccc+0000003+
ddddd+0000004+
ggggg+0000001+
ggggg+0000001+
How come?
What about the two last lines of the output?
What I'm missing?
gnu sort has the same behavior, but it has a -g option which behaves properly. Is there anything odd with -n option?
(HP-UX noah B.11.00 U 9000/800 638329302 unlimited-user license)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 07:17 AM
05-22-2002 07:17 AM
Re: numeric sort
I think you want:
# sort -n -k 1.6 -k 1.13 myfile
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 07:24 AM
05-22-2002 07:24 AM
Re: numeric sort
ggggg-0000004-
eeeee-0000003-
fffff-0000002-
ggggg-0000001-
aaaaa+0000001+
ggggg+0000001+
ggggg+0000001+
bbbbb+0000002+
ccccc+0000003+
ddddd+0000004+
Sorts on the sign first, then the number. The -n option is probably freaking out on the fact that "+" and "-" aren't numbers.
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 07:45 AM
05-22-2002 07:45 AM
Solutiondependant on what you need to do with the results you could do something like this strip out the - and + then sort works fine.If you need the plus and minus in the output this is not a lot of ggod.
cat filename | tr "-" " " | tr "+" " " | sort -n +1
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 07:55 AM
05-22-2002 07:55 AM
Re: numeric sort
this will do it
cat filename | tr "-" " - " | tr "+" " + " | sort -n +1 | tr " - " "-" | tr " + " "+"
long winded but works.
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 09:34 AM
05-22-2002 09:34 AM
Re: numeric sort
What drives to confusion is the fact that the manual states that +000.. will be treated as -000.. which implies the possible use of plus as starting sign.
The proper solution seems to me replacing + with blank or zero.
Thank you anywa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 10:32 AM
05-22-2002 10:32 AM
Re: numeric sort
Ah, thank you. In that case (using your input data):
# sort -k 1.6r,1.6r -k 1.7n,1.13n -k 1.1,1.5 /tmp/sort
...would yield:
ggggg-0000001-
fffff-0000002-
eeeee-0000003-
ggggg-0000004-
aaaaa+0000001+
ggggg+0000001+
ggggg+0000001+
bbbbb+0000002+
ccccc+0000003+
ddddd+0000004+
BTW, I see that you joined the Forum today. Welcome!!!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 10:51 AM
05-22-2002 10:51 AM
Re: numeric sort
Oh, brain death. I keep looking at the sign on the right (after) the number *not* before, so disregard the last offering.
The problem is nasty because the "-" sign appears higher (\055) in the collating sequence than the "+" sign {\053). Worse yet, is that we want a descending numeric sort for negative numbers and an ascending one for positive ones.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 10:57 AM
05-22-2002 10:57 AM
Re: numeric sort
yes I will get that result, which is not what is intended.
In numeric sorting negative number sort "descending" according to absolute value.
Positive numbers sort "ascending" according to absolute value. Managing absolute values as string is OK as long as you don't have decimal points.
Mixing positive and negative values always needs numeric sorting with no tricks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 11:07 AM
05-22-2002 11:07 AM
Re: numeric sort
are the numbers posative and negative depending on plus or minus sign im sure we can still manipulate the file to give the right sequence ?
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 11:39 AM
05-22-2002 11:39 AM
Re: numeric sort
This will do it while we look for a more elegant solution based on REs:
awk '{
if (substr($0,6,1) == "+")
print substr($0,1,5) " " substr($0,7) ;
else
print $0 ;
}' | sort -k1.6n,1.13
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 11:51 AM
05-22-2002 11:51 AM
Re: numeric sort
ok here we go again but you have to loose the plus signs and the trailing minus.
cat filename | tr "+" " " | sed -e 's/-/ -/g' | awk '{ print $1 " " $2 }' | sort +n +1
cheeers
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 11:54 AM
05-22-2002 11:54 AM
Re: numeric sort
I didn't see your last response before I submitted my last thoughts.
good luck
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2002 12:02 PM
05-22-2002 12:02 PM
Re: numeric sort
and your awk will behave erratically if there are more blanks in the record.
Please, don't take my example as the real files. It was intended just to example.
Take
US3456357+0003.89-456A234
DE3562990-00233.3+4555666
DE34+++78+003.302N7888888
this col.^ to ^
First three plus signs of last record should not be lost
neither the last in the second record.
BTW what about a nice ERE to substitute ONLY the sixth char.