1748060 Members
5072 Online
108758 Solutions
New Discussion юеВ

Awk query

 
SOLVED
Go to solution
user57
Occasional Advisor

Awk query

Hi,

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
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Awk query

Hi:

# grep hdiskpower file|sort

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Awk query

Hi (again):

Oops, sorry; disregard the first post. You want to order _by_number_:

# grep hdiskpower file|sort -nk 1.11

...which makes the sort key numeric beginning with the eleventh (one-relative) character to the end of the line.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

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.
user57
Occasional Advisor

Re: Awk query

Thanks for the solutions
James R. Ferguson
Acclaimed Contributor

Re: Awk query

Hi (again):

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...
melvyn burnard
Honored Contributor

Re: Awk query

Also, in future do not open two threads with the same subject.
My initial view was these were duplicate, and I was about to delete one.
My house is the bank's, my money the wife's, But my opinions belong to me, not HP!