awk pipe to sort


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pipe to sort
# 1  
Old 11-16-2015
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 Smilie.

Code:
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[i] / c[i]
        printf("%s:%s\t%s\t%s\n", a[i], b[i], i, avg | sort -n)
    }
}

# 2  
Old 11-16-2015
It's fine as is, but double quote the sort -n command.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-16-2015
Lose the parentheses (and use double quotes like RudiC suggested). Try:
Code:
printf "%s:%s\t%s\t%s\n", a[i], b[i], i, avg" | "sort -n"

rather than:
Code:
printf("%s:%s\t%s\t%s\n", a[i], b[i], i, avg | sort -n)

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 11-16-2015
I get:

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

Thank you Smilie.

edit: is it:

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

the output produced is: (doesn't look like $3 is sorted, that is the avg) Thank you Smilie.
Code:
chr14:21756119    RPGRIP1-11    195.412
chr14:21762826    RPGRIP1-12    216.178
chr14:21769115    RPGRIP1-13    151.347
chr14:21770636    RPGRIP1-14    194.084


Last edited by cmccabe; 11-16-2015 at 05:26 PM.. Reason: added edit
# 5  
Old 11-16-2015
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
Code:
printf "%s:%s\t%s\t%s\n", a[i], b[i], i, avg | "sort -k3,3n"

instead of:
Code:
printf "%s:%s\t%s\t%s\n", a[i], b[i], i, avg | "sort -n"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort file using pipe

I am trying to create a file (epilepsy70_average.txt) and then pipe that file into a sort and save a new file. The new file is sort.txt but as of know it is blank. I can create the file in one command and then sort it in another. Is the pipe not correct? Thank you :). awk... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. SuSE

Is this iostat pipe to sort service request command correct?

Hi, my os is SLES 10 from sort iostat as my output does not include rkB/s and wkB/s I probably have to adjust the key position, so is the following iostat pipe to sort service request command correct? oracle@vmc_stallite:~> iostat -x | sort -nrk11 sda 0.96 ... (4 Replies)
Discussion started by: jediwannabe
4 Replies

3. Shell Programming and Scripting

BOTH pipe and file(s) into awk

Hello all, quick question: is it possible to pass input into AWK BOTH with a pipe AND a file at the same time, something like this: command .......|awk '.................' FILEIN > fileout All I read says either one or the other, not both, is it at all possible? And how would the... (2 Replies)
Discussion started by: gio001
2 Replies

4. Shell Programming and Scripting

awk print pipe

Hey fellas, I wrote an script which its output is like this: a 1 T a 1 T a 2 A b 5 G b 5 G b 5 G I wanna print $1 $2 and the total number of $2 value as the third column and after that $3. Sth like this: a 1 2 T a 2 1 A b 5 3 G I know how to do it with a given input... (4 Replies)
Discussion started by: @man
4 Replies

5. Shell Programming and Scripting

help with sed or awk with less pipe

<tr><th align=right valign=top>Faulty_Part</th><td align=left valign=top>readhat version 6.0</td></tr> <tr><th align=right valign=top>Submit_Date</th><td align=left valign=top>2011-04-28 02:08:02</td></tr> .......(a long string) I want to get all the field between "left valign=top>" and "... (2 Replies)
Discussion started by: yanglei_fage
2 Replies

6. Shell Programming and Scripting

sort text having delimiter with "|" (pipe)

i am having text file below NARGU S S 12358 SALES REP |22| Acccount/s RAJU R B 64253 SALES REP |12| Acccount/s RUKMAN S 32588 SALES REP |10| Acccount/s NARGUND S S 12356... (3 Replies)
Discussion started by: suryanarayana
3 Replies

7. Shell Programming and Scripting

Pipe to awk to variable

Hi! If I'm trying something like: echo "hello world" | myvar=`awk -F "world" '{print $1}'` echo $myvar myvar is always empty :confused: I googled for houres now and don't understand why it isn't working... Trying it in normal bash. Can someone explain it to me so I can say "Of course!... (8 Replies)
Discussion started by: al0x
8 Replies

8. Shell Programming and Scripting

How to pass a field from awk in a pipe?

Thanks in advance : ) I try for a long time searching for a way to split a large gzip csv file into many gzip files (except for the last sub-file which is to joint the next big file's children.) All the subfiles are to be named by the field. But I only managed to split them into the... (9 Replies)
Discussion started by: Kingsley
9 Replies

9. Shell Programming and Scripting

Is it better to grep and pipe to awk, or to seach with awk itself

This may just be a lack of experience talking, but I always assumed that when possible it was better to use a commands built in abilities rather than to pipe to a bunch of commands. I wrote a (very simple) script a while back that was meant to pull out a certain error code, and report back what... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

10. UNIX for Dummies Questions & Answers

help needed with sort. pipe and popen()

Suppose I want to find the number of uniq lines in a file. I use the following command: sort file1 | uniq -c | wc -l But if for some reason sort fails, the above command returns 0 as the answer. Why would sort fail ? sort makes use of directories /tmp or /var/tmp to store temporary files.... (7 Replies)
Discussion started by: the_learner
7 Replies
Login or Register to Ask a Question