- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Awk query
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
Discussions
Discussions
Forums
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
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
тАО09-09-2009 06:44 AM
тАО09-09-2009 06:44 AM
I'm looking for a routine to sort 'hdiskpower' disk by number.
Input:
hdisk5
hdisk22
hdisk23
hdisk29
hdiskpower12
hdisk12
hdisk11
hdisk10
hdiskpower22
hdiskpower32
hdisk8
hdisk6
hdisk20
hdiskpower13
hdiskpower27
hdiskpower43
Desire output:
hdiskpower12
hdiskpower13
hdiskpower22
hdiskpower27
hdiskpower32
hdiskpower43
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-09-2009 06:55 AM
тАО09-09-2009 06:55 AM
Re: Awk query
# grep hdiskpower file|sort
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-09-2009 08:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-09-2009 10:06 AM
тАО09-09-2009 10:06 AM
Re: Awk query
As JRF outlines, the regular sort has enough power to solve this if the data is either fixed columns and/or in easy delimited fields. If there is no clear delimiter, then maybe you can add one just for the sort, do the sort, and remove the delimiter.
Something like:
# perl -pe "s/(\d)/~\1/" tmp.txt | sort -t~ -k1,2.n | perl -pe "s/~//"
For more complex comparisons I often prefer to explicitly peel apart the the fields and use the desired transformations on them perl.
For example for this simple case:
As a 'one-liner' :
$ perl -ne 'm/(^\D+)(\d+)$/;$nam{$.}=$1;$num{$.}=$2 }{ for (sort {$nam{$a} cmp $nam{$b} or $num{$a}
<=> $num{$b}} keys %nam) { print "$nam{$_}$num{$_}\n"}' tmp.txt
hdisk5
hdisk6
hdisk8
hdisk10
hdisk11
hdisk12
hdisk20
hdisk22
hdisk23
hdisk29
hdiskpower12
hdiskpower13
hdiskpower22
hdiskpower27
hdiskpower32
hdiskpower43
In slow motion
$ perl -ne ' # loop over input, do no print
m/(^\D+)(\d+)$/; # match on non-decimal and decimals
$nam{$.}=$1; # store the name part
$num{$.}=$2 # store the numeric part
}{ # end the read block, start the end block
for (sort { # sort with function being...
$nam{$a} cmp $nam{$b} # first by name
or # if 0 = equal then
$num{$a} <=> $num{$b} # next by numbers
} keys %nam) # to be sorted is the keys (line numbers)
{ print # action!
"$nam{$_}$num{$_}\n" # reconstruct line
}' tmp.txt # and action, end command, input
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-09-2009 11:46 PM
тАО09-09-2009 11:46 PM
Re: Awk query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-10-2009 03:16 AM
тАО09-10-2009 03:16 AM
Re: Awk query
Hi (again):
I see that you are new to this Forum. Welcome!
That said, please read the link below about points. Points are not only a way of saying "thanks!" but bread-crumbs for future trollers to find the tastiest (most applicable) solution:
http://forums.itrc.hp.com/service/forums/helptips.do?#28
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-10-2009 03:52 AM
тАО09-10-2009 03:52 AM
Re: Awk query
My initial view was these were duplicate, and I was about to delete one.