awk pipe to sort

In the below awk to add a sort by smallest to largest should it be added after the END? Thank you :).

BEGIN {
  FS="[ \t|]*"
}
# Read search terms from file1 into 's'
FNR==NR {
    s[$0]
    next
}
{
    # Check if $5 matches one of the search terms
    for(i in s) {
        if($5 ~ i) {

            # Store first two fields for later usage
            a[$5]=$1
            b[$5]=$2

            # Add $9 to total of $9 per $5
            t[$5]+=$8
            # Increment count of occurences of $5
            c[$5]++

            next
        }
    }
}
END {

    # Calculate average and print output for all search terms
    # that has been found
    for( i in t ) {
        avg = t / c
        printf("%s:%s\t%s\t%s\n", a, b, i, avg | sort -n)
    }
} 

It's fine as is, but double quote the sort -n command.

1 Like

Lose the parentheses (and use double quotes like RudiC suggested). Try:

printf "%s:%s\t%s\t%s\n", a, b, i, avg" | "sort -n"

rather than:

printf("%s:%s\t%s\t%s\n", a, b, i, avg | sort -n)
1 Like

I get:

awk: /home/cmccabe/Desktop/match.awk:33:         printf "%s:%s\t%s\t%s\n", a, b, i, avg" | "sort -n" 

Thank you :).

edit: is it:

 printf "%s:%s\t%s\t%s\n", a, b, i, avg | "sort -n" 

the output produced is: (doesn't look like $3 is sorted, that is the avg) Thank you :).

chr14:21756119    RPGRIP1-11    195.412
chr14:21762826    RPGRIP1-12    216.178
chr14:21769115    RPGRIP1-13    151.347
chr14:21770636    RPGRIP1-14    194.084 

If you want to sort on the 3rd field instead of the first field, you have to tell sort to sort on the 3rd field. Try

printf "%s:%s\t%s\t%s\n", a, b, i, avg | "sort -k3,3n"

instead of:

printf "%s:%s\t%s\t%s\n", a, b, i, avg | "sort -n"