user14748664
user14748664

Reputation:

AWK: sorting line based on the column data

I am dealing with the CSV file produced from > 1000 txt filles, taking from each of the file its name and the value from the 5th column using the following AWK script:

 awk -F', *' 'FNR==2 {f=FILENAME; 
                     sub(/.*\//,"",f);
                     sub(/_.*/ ,"",f);
                     printf("%s: %s\n", f, $5) }' "${tmp}"/*.txt >> ${home}/output.csv

Here is a part of the output.CSV file, which acually consist 1020 lines according to the number of lig, based on which I need to sort this data:

 lig1000: -7.5800
    lig1001: -4.8400
    lig1002: -7.7200
    lig1003: -4.8400
    lig1004: -7.9800
    lig1005: -7.5200
    lig1006: -6.0700
    lig1007: -7.3100
    lig1008: -7.7200
    lig1009: -7.3700
    lig100: -5.1400
    lig1010: -4.6600
    lig1011: -8.1500
    lig1012: -7.6100
    lig1013: -7.0200
    lig1014: -7.4100
    lig1015: -5.8700
    lig1016: -6.8400
    lig1017: -5.5300
    lig1018: -5.4100
    lig1019: -6.6900
    lig101: -6.2700
    lig1020: -6.2600
    lig1021: -4.0000
    lig1022: -5.9200
    lig1023: -8.0200
    lig1024: -7.5800
    lig1025: -4.2100
    lig1027: -7.0500
    lig1028: -6.1700
    lig1029: -4.9700
    lig997: -6.7000
    lig998: -9.1800
    lig999: -7.3000
    lig99: -5.2700
    lig9: -6.1400

The problem is in the order of the linnes, whhich I would like to sort from lig1 to lig1021 (last) automatically during AWK procession. I've tried to pipe the AWK expression to | LC_ALL=C sort -t':' -k1,1g which normally works on my mac osx but it produced the same order. How I could modify my sorting command? Thanks!

Upvotes: 1

Views: 121

Answers (1)

hek2mgl
hek2mgl

Reputation: 157992

I suggest to pipe the output to sort:

awk '...' input.csv | sort -k1.4,1n

-k KEYDEF is used to the define the sort key:

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character posi‐ tion in the field; both are origin 1, and the stop position defaults to the line's end.

That means: -k1.4,1n will perform a numeric sort, based on the first field, starting at the 4th character of that field

Upvotes: 1

Related Questions