Need help on horizontal sort with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on horizontal sort with AWK
# 1  
Old 06-05-2013
Need help on horizontal sort with AWK

dear friends..

i have data :
Code:
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
Code:
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
# 2  
Old 06-05-2013
Does it have to be AWK?
Code:
perl -aF";" -ple '$_=join ";", (@F[0..4],sort {$b <=> $a} @F[5..$#F])' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-05-2013
Here's an awk script you can try...
Code:
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.

Last edited by shamrock; 06-05-2013 at 01:31 PM..
This User Gave Thanks to shamrock For This Post:
# 4  
Old 06-05-2013
Quote:
Originally Posted by shamrock
Here's an awk script you can try...
Code:
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
# 5  
Old 06-05-2013
Another approach using gawk asort function:
Code:
gawk '
        {
                c = 0
                n = split ( $0, A, ";" )
                for ( i = 1; i <= n; i++ )
                {
                        if ( i <= 5 )
                                s = s ? s OFS A[i] : A[i]
                        if ( i >= 6 )
                                S[++c] = A[i]
                }
                n = asort ( S )
                for ( i = n; i >= 1; i-- )
                        s = s OFS S[i]
                print s
                s = ""
                delete S
        }
' OFS=";" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to change comma separated line to horizontal

I am trying to change a file that looks like this: file, announcement,date, server, server01, server02, server06, file04, rec01, rec04, rec03... etc into a vertical file like this: file announcement date server server01 server02 server06 The file does not have to be sorted... (5 Replies)
Discussion started by: newbie2010
5 Replies

2. Shell Programming and Scripting

Script awk vertical to horizontal with some condition

hi all.. i have problem, right now i want to processing some data with input like this BIMAB ACF-0168 QTS-0465 QUA 2013-08-17 14:16:09.34 ** ALAR ORX -004 NDORIDUNGGA (21943) 7745 ABOVE DEFINED 02 00 01 00 00 00 01 00 00 01 03... (12 Replies)
Discussion started by: buncit8
12 Replies

3. Shell Programming and Scripting

awk in horizontal and vertical math

Based on input ail,UTT,id1_0,COMBO,21,24,21,19,85 al,UTHAST,id1_0,COMBO,342,390,361,361,1454 and awk code as awk -F, '{ K=0; for(i=NF; i>=(NF-4); i--) { K=K+$i; J=J+$i;} { print K } } END { for ( l in J ) printf("%s ",J); }' I'm trying to add columns and lines in single line. line... (6 Replies)
Discussion started by: busyboy
6 Replies

4. Shell Programming and Scripting

awk Help: Horizontal to Vertical print with pattern match

Hi AWK Experts, Following is the data : BRH113 DD AA HH CA DD DD AA HH BRH091 A4 A6 AH H7 67 HH J8 9J BRH0991 AA D8 C23 V5 H7 BR2 BRH991 AA HH GG5 BT0 JJ0 I want the output to be alligned with the pattern matching "BRH" inthe line. The output should be look like: A]... (4 Replies)
Discussion started by: rveri
4 Replies

5. Shell Programming and Scripting

awk - horizontal and vertical text extraction

Hi, I need some help in getting extracting the specific horizontal and vertical texts in a single line. I am trying to extract few of the parameters from a config file. Your help is appreciated. Desired Output ---------------- Pool members members ... (4 Replies)
Discussion started by: pratheeshp
4 Replies

6. Shell Programming and Scripting

Need perl or shell script to sort vertical lines to horizontal line in csv format

Need perl or shell script to sort vertical lines to horizontal line in csv format My file like below ------------------------- ================================================================================ PATH PINKY1000#I1-1-ZENTA1000-2#I7-1-ASON-SBR-UP-943113845 ... (4 Replies)
Discussion started by: sreedhargouda.h
4 Replies

7. Shell Programming and Scripting

awk sort

input file abc1 abc23 abc12 abc15 output abc1 abc12 abc15 abc23 (9 Replies)
Discussion started by: yanglei_fage
9 Replies

8. Shell Programming and Scripting

Sort in AWK

Hi, I usually use Access to sort data however for some reason its not working. Our systems guys and myself cannot figure it out so ive tried to use AWK to do the sorting. The file is made up of single lines in the format ... (4 Replies)
Discussion started by: eknryan
4 Replies

9. Homework & Coursework Questions

awk sort help

1. The problem statement, all variables and given/known data: I dont know what I do wrong, I am trying to create shell programming database: I have this command first: && > $fname ... echo $Name:$Surname:$Agency:$Tel:$Ref: >> $fname then I have echo " Name Surname Agency Tel... (2 Replies)
Discussion started by: jeht
2 Replies

10. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies
Login or Register to Ask a Question