awk print output problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print output problem
# 1  
Old 05-20-2014
awk print output problem

Hello friends,

I have written a script and i need to add some part into it so that i could print out more results depending on more conditions,

This is the core part of the script which does the actual work:

Code:
echo "$j" && nawk -v stat=$2 'NR==FNR && $1==stat{STATE=$2}/State=\[stat\]Action.*in/gsub(/^\[|\]/,"",$(NF-1)){sum+=$(NF-1);avg=(sum/NR)} END { print "Avg Proc Time for " STATE " =",avg}' state.cfg $j;


Code:
$j

is an input which is coming from output of a previously run find command:
Code:
/data/log/rfe/profile3.log
/data/log/rfe/profile4.log
/data/log/rfe/profile3.log.2014-05-20-00
/data/log/rfe/profile4.log.2014-05-20-00
/data/log/rfe/profile3.log.2014-05-20-01
/data/log/rfe/profile4.log.2014-05-20-01
/data/log/rfe/profile3.log.2014-05-20-02

I need to mark result in output which are higher than, lets say "250" with a star sign. So, desired output is:

Code:
-bash-3.00$ cat avg_proc_time_results.txt 
/data/log/rfe/profile3.log.2014-05-20-00
Avg Proc Time for Charging = 199.98
/data/log/rfe/profile4.log.2014-05-20-00
Avg Proc Time for Charging = 200.021
/data/log/rfe/profile3.log.2014-05-20-01
Avg Proc Time for Charging = 214.448
/data/log/rfe/profile4.log.2014-05-20-01
Avg Proc Time for Charging = 213.565
/data/log/rfe/profile3.log.2014-05-20-02
Avg Proc Time for Charging = 170.646
/data/log/rfe/profile4.log.2014-05-20-02
Avg Proc Time for Charging = 168.457
/data/log/rfe/profile3.log.2014-05-20-03
Avg Proc Time for Charging = 265.9*
/data/log/rfe/profile4.log.2014-05-20-03
Avg Proc Time for Charging = 258.402*

Note: Current output file is just the one without star marks.
I have tried this

Code:
echo "$j" && nawk -v stat=$2 'NR==FNR && $1==stat{STATE=$2}/State=\[stat\]Action.*in/gsub(/^\[|\]/,"",$(NF-1)){sum+=$(NF-1);avg=(sum/NR)} END {if(avg > 250) print "Avg Proc Time for " STATE " ="avg"*"}' state.cfg $j;

but it is only giving the following output; not all results..
Code:
/data/log/rfe/profile3.log.2014-05-20-00
/data/log/rfe/profile4.log.2014-05-20-00
/data/log/rfe/profile3.log.2014-05-20-01
/data/log/rfe/profile4.log.2014-05-20-01
/data/log/rfe/profile3.log.2014-05-20-02
/data/log/rfe/profile4.log.2014-05-20-02
/data/log/rfe/profile3.log.2014-05-20-03
/data/log/rfe/profile4.log.2014-05-20-03
/data/log/rfe/profile3.log.2014-05-20-04
Avg Proc Time for Charging =265.9*
/data/log/rfe/profile4.log.2014-05-20-04
Avg Proc Time for Charging =258.402*
/data/log/rfe/profile3.log.2014-05-20-05

I have tried more thigns but could not achieve it,
i would appreciate any help,
thanks in advance,

KR
Eagle

Last edited by EAGL€; 05-20-2014 at 10:06 AM..
# 2  
Old 05-20-2014
You are printing only if avg > 250. Add an else condition and print without '*'

---------- Post updated at 10:31 AM ---------- Previous update was at 10:29 AM ----------

Code:
END {if(avg > 250) {print "Avg Proc Time for " STATE " ="avg"*"} else {print "Avg Proc Time for " STATE " ="avg}}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print commas between awk output

When I output fields 1 2 4 5 & 6, I would like to have a comma between them but I am beating my head against the wall to get it to work. Any help is appreciated sed 's/]*,]*/,/g' file1 > file1.$$ && awk -F, 'FNR==NR{f2=$1 $2 $4 $5 $6;next} FNR==1{print $0, "CDP NE Hostname,CDP NE IP,Remote... (6 Replies)
Discussion started by: dis0wned
6 Replies

2. UNIX for Dummies Questions & Answers

Any awk one liner to print df output?

Hi, OS = Solaris Can anyone advise if there is a one liner to print specific output from a df -k output? Running df from a command line, it sometimes gives me 2 lines for some volume. By re-directing the output to a file, it always gives 1 line for each. Below is an example output,... (4 Replies)
Discussion started by: newbie_01
4 Replies

3. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

4. Shell Programming and Scripting

problem with print append to output file syntax

I'm trying to output the contents of the infile to the outfile using Append. I will want to use append but the syntax doesn't seem to be working ! Input file (called a.txt) contains this: a a a b b b I'm running shell script (called k.sh) from Unix command-line like this: ./k.sh .... (1 Reply)
Discussion started by: script_op2a
1 Replies

5. Shell Programming and Scripting

awk subtraction and print output

FileA F97S 83 530 K569E 531 736 output shud be F16S K40E it is code sed 's///g' FileA |awk '{print $1-$2+2}' it will print 16 40 anything can come with this output?? (1 Reply)
Discussion started by: cdfd123
1 Replies

6. UNIX for Dummies Questions & Answers

awk {print $NF} output??

Hi, I am trying to debug an old script and have found the problem lies within this function: isIdoc() { # validate the file type fileType=`file $1 | awk '{print $NF}'` && echo 0 || echo 1 } My question is, how can I determine what is in the variable $fileType ? The program is... (1 Reply)
Discussion started by: vervette
1 Replies

7. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

8. Shell Programming and Scripting

using perl or awk to print output

suppose u have file File A A -> G C->D A -> R P->A File B A=1 C=2 D=3 E=4 F=5 G=6 H=7 I=8 K=9 L=10 M=11 (5 Replies)
Discussion started by: cdfd123
5 Replies

9. Shell Programming and Scripting

AWK Merge Fields for Print Output

I've got a file with each record on a separate line and each record contains 34 fields separated by a colon and i'm trying to re-arrange the order of the fields and merge together certain fields separated by a slash (like field7/field28). I tried using an awk print statement like awk -F: 'BEGIN... (5 Replies)
Discussion started by: RacerX
5 Replies

10. Shell Programming and Scripting

using awk to search and print output

suppose i have one file file A 18 24 30 35 38 45 55 Another file file B 08_46 A 16 V -0.36 0.23 E : 1.41 08_46 A 17 D -1.04 0.22 E : 0.84 08_46 A 18 Q -0.49 0.12 E : 0.06 08_46 A 19 G 0.50 0.14 E : 0.05 08_46 A 20 V ... (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question