dear friends..
i have data :
20130603;ABCD;ABCD1;14030;51271;0.000;0.000;21.000
20130603;ABCD;ABCD2;14030;51272;4853.000;53591.000;40539.000
20130603;ABCD;ABCD3;14030;51273;38024.000;385068.000;396424.000
20130603;ABCD;EFGH1;14030;51334;285879.000;563964.000;141780.000
20130603;ABCD;EFGH2;14030;51335;326417.000;526374.000;71600.000
20130603;ABCD;EFGH3;14030;51336;351495.000;1044648.000;679212.000
I wanna horizontally sort each line start from $6 descending,
with output
20130603;ABCD;ABCD1;14030;51271;21.000;0.000;0.000
20130603;ABCD;ABCD2;14030;51272;53591.000;40539.000;4853.000
20130603;ABCD;ABCD3;14030;51273;396424.000;385068.000;38024.000
20130603;ABCD;EFGH1;14030;51334;563964.000;285879.000;141780.000
20130603;ABCD;EFGH2;14030;51335;526374.000;326417.000;71600.000
20130603;ABCD;EFGH3;14030;51336;1044648.000;679212.000;351495.000
i have searching n try some awk script but still failed.
i really appreciate your help
thanks
Does it have to be AWK?
perl -aF";" -ple '$_=join ";", (@F[0..4],sort {$b <=> $a} @F[5..$#F])' file
1 Like
Here's an awk script you can try...
awk '{
n = split($0, a, ";")
for (i = 1; i < (n + 1) - 6 ; i++)
for (j = 6; j < (n + 1) - i; j++)
if (a[j] < a[j+1]) {
t = a[j]
a[j] = a[j+1]
a[j+1] = t
}
for (k = 1; k <= n; k++)
printf("%s%s", a[k], k < n ? ";" : "\n")
}' file
Caveat is that...it assumes that part of the line that needs sorting starts at $6.
1 Like
shamrock:
Here's an awk script you can try...
awk '{
n = split($0, a, ";")
for (i = 1; i < 3; i++)
for (j = 6; j < n + 1 - i; j++)
if (a[j] < a[j+1]) {
t = a[j]
a[j] = a[j+1]
a[j+1] = t
}
for (k = 1; k <= n; k++)
printf("%s%s", a[k], k < n ? ";" : "\n")
}' file
awesome... its work
thanks a lot
Yoda
June 5, 2013, 12:30pm
5
Another approach using gawk asort function:
gawk '
{
c = 0
n = split ( $0, A, ";" )
for ( i = 1; i <= n; i++ )
{
if ( i <= 5 )
s = s ? s OFS A : A
if ( i >= 6 )
S[++c] = A
}
n = asort ( S )
for ( i = n; i >= 1; i-- )
s = s OFS S
print s
s = ""
delete S
}
' OFS=";" file