[CentOS] tricky perl question - ascending order

Eduardo Grosclaude eduardo.grosclaude at gmail.com
Sun May 23 20:39:37 UTC 2010


On Sun, May 23, 2010 at 1:15 PM, Jozsi Vadkan <jozsi.avadkan at gmail.com> wrote:
> or maybe in bash..
> script/"one liner" e.g.: input: http://pastebin.com/raw.php?i=pMZPEsMZ
>
> i want to make this output from it:
> http://pastebin.com/raw.php?i=kH8VxT0A
>
>
> So from the input, i want to make an ascendant order, how many things
> are under a "SOMETHING-XX"
>
> Does anyone has any "perl magic" in the pocket, how to do this? :D

Ok, I'l bite, hope John D. isn't looking...

This is as close as I can get. I shall confess I'm cheating; I had to
add two extra blank lines at the beginning of your file. Don't know if
allowed.
Also, SOMETHING-blocks look collapsed; don't know if bug or feature
for you :D Anyway, it can make for a start.

perl -ne '$/="\n\n"; @a=split(); push
@b,({q=>scalar(@a),l=>[@a]});END{for $i (sort {$a->{q} <=> $b->{q}}
@b) {print "@{$i->{l}}\n"}}' < raw.txt

SOMETHING-85-uj sfvtc
SOMETHING-86 yxcver
SOMETHING-88 yxfrf yxwwcc
SOMETHING-82-H sdfyxcv asdfyxcv asdfgyx
SOMETHING-82 lajsldfj alsdfjlas djfalsd asdfasd dsfx
SOMETHING-85 xdfvqe asdwe yxccv cxvbe wdyfv qyxvvy yxcvtg yxcvgt

For the sake of really helping, let's analyze a bit:

$/= we are replacing record separator for split()
@a = split() split in list context, give me the actual lines
$a = scalar(@a) give me the number of lines
@b is a list to throw our (#lines, lines) pairs into
{q=>$a,l=>[@a]} is a hash with two keys, q (#lines) and l (lines)
END{} when all else is done
sort {$a->{q} <=> $b->{q}} @b give me b sorted by q
print "@{$i->{l}}\n" print each l element as a list

HTH

-- 
Eduardo Grosclaude
Universidad Nacional del Comahue
Neuquen, Argentina



More information about the CentOS mailing list