bash - Using an awk script on multiple rows of a file -


i have following file: (not sure why it's not aligned here... in terminal...)

name        state   pay rate    hours worked    overtime hours   john doe    mo  13.75       27      0 jane doe    ks  21.00       32      0 sam smith   mo  32.50       40      8 barb jones  mo  28.25       40      16 jenny lind  ks  10.50       28      0 

so wrote script:

#!/bin/awk -f  {         name=$1" "$2         state=$3         payrate=$4         hoursworked=$5         overtime=$6          grosspay=(hoursworked+(overtime*1.5))*payrate         if (state == "ks")                 tax = grosspay* .07         else if (state == "mo")                 tax = grosspay* .08         else                 print "tax info not found"          netpay = grosspay-tax } end{         print "name    \tstate\n",         name, "\t", state,         "\ngross pay:\t$", grosspay,         "\ntaxes:     \t$", tax,         "\nnet pay:\t$", netpay } 

which produces following: (not sure why there space before jenny...)

name        state  jenny lind      ks  gross pay:  $ 294  taxes:      $ 20.58  net pay:    $ 273.42 

my question if there way loop through , use "fnr" print results of of different rows print same output barb jones, sam smith, etc. stopping @ name.

sorry if dumb question


edit:

so changed script to:

#!/bin/awk -f  {         name=$1" "$2         state=$3         payrate=$4         hoursworked=$5         overtime=$6          grosspay=(hoursworked+(overtime*1.5))*payrate         if (state == "ks")                 tax = grosspay* .07         else if (state == "mo")                 tax = grosspay* .08         else                 print ""          netpay = grosspay-tax         print "\nname    \tstate\n",         name, "\t", state,         "\ngross pay:\t$", grosspay,         "\ntaxes:     \t$", tax,         "\nnet pay:\t$", netpay } end{         print "\n-complete-" } 

and output:

name        state  name state      pay  gross pay:  $ 0  taxes:      $   net pay:    $ 0  name        state  john doe    mo  gross pay:  $ 371.25  taxes:      $ 29.7  net pay:    $ 341.55  name        state  jane doe    ks  gross pay:  $ 672  taxes:      $ 47.04  net pay:    $ 624.96  name        state  sam smith   mo  gross pay:  $ 1690  taxes:      $ 135.2  net pay:    $ 1554.8  name        state  barb jones      mo  gross pay:  $ 1808  taxes:      $ 144.64  net pay:    $ 1663.36  name        state  jenny lind      ks  gross pay:  $ 294  taxes:      $ 20.58  net pay:    $ 273.42 

which 95% need how can ignore first line "name, state, etc." , still can't figure out why adds space in front of names.

some suggested improvements:

begin {     split("ks .07 mo .08",tmp)     (i=1;i in tmp;i+=2)         taxrate[tmp[i]] = tmp[i+1]     fmts = "%12s%s\n"     fmtf = "%12s$%.2f\n" } nr>1 {     name=$1" "$2     state=$3     payrate=$4     hoursworked=$5     overtime=$6      grosspay=(hoursworked+(overtime*1.5))*payrate     tax = grosspay* taxrate[state]     netpay = grosspay-tax      printf fmts, "name", "state"     printf fmts, name, state     printf fmtf, "gross pay:", grosspay     printf fmtf, "taxes:", tax     printf fmtf, "net pay:", netpay } end {     print "\n-complete-" } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -