[NTLUG:Discuss] Sort question

Fred James fredjame at fredjame.cnc.net
Wed Apr 18 19:44:49 CDT 2012


Chris Cox wrote:
> On 04/05/2012 07:39 PM, Fred James wrote:
>> Chris Cox wrote:
>>> first tr should be '[0-9]' get rid of the carat
>>>
>>>> Chris Cox wrote:
>>>>> My soln in bourne shell script:
>>>>> #!/bin/sh
>>>>> while read line;do
>>>>> echo "$line" | tr -cs '[^0-9]' '\012' | sort -n | tr '\012' ' '
>>>>> echo ""
>>>>> done
>>>>>
>>>>> To use: sh thescript.sh <thedatafile
>>>>>
>>>>>
>>>> Very nice
>>>> Regards
>>>> Fred James
>> Chris Cox
>> I had wondered about the carat ... but it works in either case ... 
>> ¿Por qué, me
>> pregunto?
>
> A reasonable question... you could tell had forgotten about the -c 
> option to complement the character set.. maybe that's the "answer" if 
> you will... the reason why.. maybe carat has no meaning in that case 
> since it's complementing the whole set?  Just a guess.  The -s 
> eliminates the extra non numerics (things in the complemented set) no 
> matter how many there are (reduces them down to 1, in case anyone 
> wonders).  In short, just give me the numbers and the -cs compresses 
> the rest to make the numbers followed by a newline... this would allow 
> the input to look like:
>
> "53","17","3","24","9","33"
>
> even.... but the output would just be the numbers sorted with space as 
> the separator... again, in case you wondered... however, will have 
> some leading white space.
Chris Cox
I am slow, eh ... here is a variation on the theme that gets rid of the 
leading space, if any ... all I added was the final sed command ...
    echo "$line" | tr -cs '[0-9]' '\012' | sort -n | tr '\012' ' ' | sed 
-e 's/^ //'
Regards
Fred James




More information about the Discuss mailing list