Error with redirection in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error with redirection in awk
# 1  
Old 12-01-2009
Error with redirection in awk

I'm trying to find average of values in 2nd column in some files. Filenames have following pattern eg:

Code:
tau_2.54_even_v1.xls
tau_2.54_odd_v1.xls
tau_1.60_v1.xls
tau_800_v1.xls

Code:
#!/bin/bash
for file in pmb_mpi tau xhpl mpi_tile_io fftw ; do
    for f in "2.54" "1.60" "800" ;do
    if [ ${f} = 2.54 ]
    then 
        for order in even odd ; do
        echo ${file}_${f}_${order}_v1.xls
        awk 'sum+=$2 ;END {print "Average = " , $sum/NR > ${file}_${f}_${order}_avrg.xls }' ${file}_${f}_${order}_v1.xls
        done
    else
        echo ${file}_${f}_v1.xls 
        awk 'sum+=$2 ;END {print "Average = " , $sum/NR > ${file}_${f}_avrg.xls }' ${file}_{f}_v1.xls
    fi

    done
done

The other filenames can be obtained by replacing variable file with oter variables pmb , xhpl etc ..
Here is the script I've written ..
Can anyone kindly find the error and let me know ?

Last edited by vishwamitra; 12-01-2009 at 05:03 AM.. Reason: code tags please
# 2  
Old 12-01-2009
Move the redirection outside the awk script :
Code:
#!/bin/bash
for file in pmb_mpi tau xhpl mpi_tile_io fftw ; do
    for f in "2.54" "1.60" "800" ;do
    if [ ${f} = 2.54 ]
    then 
        for order in even odd ; do
        echo ${file}_${f}_${order}_v1.xls
        awk 'sum+=$2 ;END {print "Average = " , $sum/NR  }' ${file}_${f}_${order}_v1.xls > ${file}_${f}_${order}_avrg.xls
        done
    else
        echo ${file}_${f}_v1.xls 
        awk 'sum+=$2 ;END {print "Average = " , $sum/NR  }' ${file}_{f}_v1.xls > ${file}_${f}_avrg.xls
    fi

    done
done

Jean-Pierre.
# 3  
Old 12-01-2009
Thanks !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk file redirection issue

So I'm writing a script which tries to parse human-readable addresses. Part of it is this: print $2, implode(A,1,AN," "), CITY, PROV, POST, COUNTRY, CITYCOUNT>2; CITYCOUNT is a variable between 0 and 3 counting the number of words in a city name. I'm trying to prnt 1 wherever that's greater... (5 Replies)
Discussion started by: Corona688
5 Replies

2. UNIX for Dummies Questions & Answers

little problem of file redirection (awk)

I almost reach my objective (Youhouuu !!!!) But I really don't understand why it doesn't work until the end... :wall: For clarity's sake I am taking a very simple example. The operations I am doing in the script (gsub and print) really don't have any importance !!! I just matter about... (10 Replies)
Discussion started by: beca123456
10 Replies

3. Shell Programming and Scripting

redirection error

Hi i am facing a very strange problem suppose the parameters which i passed to the script is -o 140 then my code is as follows echo $* | awk '{ for ( i=0;i<=NF;i++){if ($i= -o) { print ${i+1} } } ' | read abc echo $abc abc=`echo $* | awk '{ for ( i=0;i<=NF;i++){if ($i= -o) { print... (5 Replies)
Discussion started by: aishsimplesweet
5 Replies

4. Shell Programming and Scripting

awk redirection

Hi everyone i am facing a very strange problem . see below is my code #awk <do something> file 1 > file2 Now the problem is whenever i am redirecting the output to file2 it creates the file2 but of 0 size. i don't know what is the reason but it is very important to me to redirect the... (11 Replies)
Discussion started by: aishsimplesweet
11 Replies

5. Shell Programming and Scripting

awk output redirection to file

I have a system stat command running which generates data after 5 sec or so. I pass this data to awk and do some calculation to present the data differently. Once done now I want to pass this data to file as and when generated but doesn't work..unless the first command completes successfully.... (6 Replies)
Discussion started by: learnscript
6 Replies

6. Shell Programming and Scripting

sed error : Syntax error: redirection unexpected

My script is throwing the error 'Syntax error: redirection unexpected' My line of code.. cat nsstatustest.html | sed s/<tr><td align="left">/<tr><td align="left" bgcolor="#000000"><font color="white">/ > ztmp.Ps23zp2s.2-Fpps3-wmmm0dss3 HTML tags are getting in the way but they're needed to... (3 Replies)
Discussion started by: phpfreak
3 Replies

7. Shell Programming and Scripting

awk print redirection to variable file name

Hello, i need to redirect the output of print to a variable file name: #This is normal awk '{ print $17 > "output.txt" }' input #I need something like this awk '{ print $17 > "output_${25}.txt" }' input how to format the output file name to contain a variable? (6 Replies)
Discussion started by: nazeeb
6 Replies

8. UNIX for Dummies Questions & Answers

error redirection

i am using 2> to redirect all the standard errors that i get in my bash script.. this command needs to be given in all the statements for which the errors are to redirected.. is there a command that will catch all the errors in all the shell commands that are present inside a script .? pls help.. (7 Replies)
Discussion started by: sais
7 Replies

9. Shell Programming and Scripting

awk two file redirection

Hi, i use awk -F to print three variable delimited by comma $1 $2 $3 if $2=="" i want to extract this information missing from another file using awk -v + some process. but the problem i can't use the two awk together cause of redirection there's a solution. note: i can't use another... (1 Reply)
Discussion started by: kamel.seg
1 Replies

10. Shell Programming and Scripting

Tail -f, awk and redirection

I want to run tail -f to continuously monitor a log file, outputing a specific field to a second log file. I can get the first portion to work with the following command: tail -f log | awk '{if ($1 == "Rough") print $5}' also: awk '{if ($1 == "Rough") print $5}' <(tail -f log) The... (2 Replies)
Discussion started by: mfajer
2 Replies
Login or Register to Ask a Question