Stuck with awk !!! | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Stuck with awk !!!

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-14-2013
zsycho zsycho is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 13 September 2014, 12:14 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Stuck with awk !!!

Can someone help me with geting the desired output?

Source:


Code:
Bank1.sss    
     63  Result code [0] 
      2  Result code [106] 
      4  Result code [108] 
 Bank2.sss
    474  Result code [0] 
      1  Result code [103] 
      4  Result code [106] 
      2  Result code [187] 
      1  Result code [580]

Output: (Should calculate the percentage of each bank's error)


Code:
Bank1.sss
91.30     63  Result code [0] 
2.89      2  Result code [106] 
5.79      4  Result code [108] 
 Bank2.sss
97.92    474  Result code [0] 
0.20      1  Result code [103] 
0.82      4  Result code [106] 
0.41      2  Result code [187] 
0.20      1  Result code [580]

I did the following:


Code:
for i in cat FI_list; do echo $i >> err_result; grep $i CCScrape130903.log | grep "Result code"| cut -d~ -f2 | sort | uniq -c | awk '{ s += $1 } END { print "sum is", s "\n"} {printf "%f", $1/s*100} {print $0}' >> err_result; done

It is generating output as :


Code:
Bank1.sss
100.000000     63  Result code [0] 
3.076923      2  Result code [106] 
5.797101      4  Result code [108] 
sum is 69

Bank1.sss
100.000000    474  Result code [0] 
0.210526      1  Result code [103] 
0.835073      4  Result code [106] 
0.415800      2  Result code [187] 
0.207469      1  Result code [580] 
sum is 482

Can someone help me with getting the desired output?

TIA
Sponsored Links
    #2  
Old 09-14-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 October 2014, 2:03 PM EDT
Location: Zürich
Posts: 8,484
Thanks: 332
Thanked 1,047 Times in 914 Posts
Use printf "%.2f" to round to 2 decimal places (assuming that's your only problem, which I can see from the output).
Sponsored Links
    #3  
Old 09-14-2013
zsycho zsycho is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 13 September 2014, 12:14 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Scott View Post
Use printf "%.2f" to round to 2 decimal places (assuming that's your only problem, which I can see from the output).
If you see the output carefully, you will see that the output shows 100.000000 for Result code [0] even though it should not be 100, it should be 91.30 for Bank1.sss.

The first column data is the success rate in percentage for any error for a particular bank.

Eg:
For Bank 1,

total entries : 63+2+4=69
63/69*100 = 91.30
2/69*100 = 2.89 ... and so on.
    #4  
Old 09-14-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 October 2014, 2:03 PM EDT
Location: Zürich
Posts: 8,484
Thanks: 332
Thanked 1,047 Times in 914 Posts
Without any precision, it will never know how to round correctly.


Code:
$ echo "63/69*100" | bc
0
$ echo "scale=2; 63/69*100" | bc
91.00
$ echo "scale=3; 63/69*100" | bc
91.300

Sponsored Links
    #5  
Old 09-14-2013
MadeInGermany MadeInGermany is online now Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 22 October 2014, 5:37 PM EDT
Location: Simplicity
Posts: 1,973
Thanks: 127
Thanked 577 Times in 523 Posts
You get higher precision with

Code:
$1*100/s

and even higher enforcing floating point

Code:
$1*100.0/s

Sponsored Links
    #6  
Old 09-14-2013
zsycho zsycho is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 13 September 2014, 12:14 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
hmm, I am aware of that but since I am new to awk, its getting difficult for me to generate the required output.

I need to know what needs to be changed in the following command


Code:
 awk '{ s += $1 } END { print "sum is", s "\n"} {printf "%f", $1/s*100} {print $0}'

so that I can get the output as


Code:
Bank1.sss
91.30     63  Result code [0] 
2.89      2  Result code [106] 
5.79      4  Result code [108] 
 Bank2.sss
97.92    474  Result code [0] 
0.20      1  Result code [103] 
0.82      4  Result code [106] 
0.41      2  Result code [187] 
0.20      1  Result code [580]

The problem with the above awk command is :

At first, the value of s is 63. so it creates percentage as 63/63*100 = 100 %
then, the value of s is 63+2 i.e 65. so it creates percentage as 2/65*100 = 3.07 % and so on... I hope you got the point.


How can I change the command such that awk will create percentage only for the entries between ".sss" , calculate the sum and then percentage. Then move to the next loop i.e the entries between next set ".sss".

I am getting the proper sum for every set but not correct percentage.

Last edited by zsycho; 09-14-2013 at 01:36 PM..
Sponsored Links
    #7  
Old 09-14-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 October 2014, 2:03 PM EDT
Location: Zürich
Posts: 8,484
Thanks: 332
Thanked 1,047 Times in 914 Posts

Code:
awk '
  NR == FNR { if( !$2 ) { B++; next }
              BANK[B]+= $1
              next
            }
  ! $2 { T = BANK[++C]; print $0 " (total " T ")"; next }
  {
    printf( "%.2f ", $1 / T * 100 )
  } 1
' file file


Code:
Bank1.sss (total 69)
91.30      63  Result code [0] 
2.90       2  Result code [106] 
5.80       4  Result code [108] 
Bank2.sss (total 482)
98.34     474  Result code [0] 
0.21       1  Result code [103] 
0.83       4  Result code [106] 
0.41       2  Result code [187] 
0.21       1  Result code [580]

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk getting stuck after BEGIN akshaykr2 Shell Programming and Scripting 1 06-24-2009 01:04 PM
help! im stuck.. cherrywinter Shell Programming and Scripting 1 08-01-2008 01:55 PM
really stuck- need to get a variable within a variable- AWK jeffpas AIX 11 07-02-2008 10:37 PM
I'm stuck :( Legend986 Programming 4 10-14-2007 02:21 AM
stuck....! moxxx68 UNIX for Advanced & Expert Users 0 12-10-2004 10:52 PM



All times are GMT -4. The time now is 05:43 PM.