cs534: more AWK stuff
Errors
This will CERTAINLY earn you a run-time error
gawk '{ printf("%-8s $%7.2f\n", $1, $2 * $3)]' emp.dat
Simple Output
printing every line
gawk '{ print( $0) }' emp.dat
will do the same as this
gawk '{ print }' emp.dat
printing certain fields
gawk '{ print( $1, $3) }' emp.dat
will print columns 1 and 3 from the entire file
NF, the number of fields
gawk '{ print( NF, $1, $NF) }' emp.dat
will print the number of fields, and the first and last field of each record
Computing and printing
gawk '{ print( $1, $2 * $3) }' emp.dat
will print the first field, followed by the multiplication of fields $2 and $3 of each record
(printf_allPay.bash)
Printing Line Numbers
gawk '{ print( NR, $0) }' emp.dat
will print a record number (line number) before each record
(printf_lineNums.bash)
Adding text to the output
gawk '{ print "Total pay for ", $1, " is ", $2 * $3 }' emp.dat
will print "Total pay for " before each calculated record
(printf_calcTxt.bash)
Fancier (cleaner) output
gawk '{ printf("Total pay for %-8s is $%7.2f\n", $1, $2*$3 ) }' emp.dat
will print same as above but WAY cleaner output
(printf_evenCleaner.bash)
Sorting output
gawk '{ printf(" $%7.2f %s\n", $2*$3, $0 ) }' emp.dat | sort
will print a sorted listing
(printf_sort1.bash)
Selection by Comparison
gawk '$2 >= 5' emp.dat
selection based on expression above
(printf_select1.bash)
Selection by Computation
gawk '$2 * $3 > 50 { printf("$%.2f for %s\n", $2 * $3, $1) }' emp.dat
selection based on expression above
(printf_select2.bash)
Selection by Text Content
gawk '$1 == "Suzie" 'emp.dat
selection based on the string above
(printf_select3.bash)
Data Validation
gawk 'NF != 3{ print( $0, "number of fields is not equal to 3") }' corrupt_emp.dat
gawk '$2 < 3.35{ print( $0, "rate is below minimum wage") }' corrupt_emp.dat
gawk '$2 > 10{ print( $0, "rate exceeds $10 per hour") }' corrupt_emp.dat
gawk '$3 < 0{ print( $0, "negative horus worked") }' corrupt_emp.dat
gawk '$3 > 60{ print( $0, "too many hours worked") }' corrupt_emp.dat
selection(s) based on the expressions above
(printf_select4.bash)
BEGIN
The special pattern BEGIN matches before the first line of the first input file is read
This program uses BEGIN to print a heading before the file is read
gawk 'BEGIN { print "NAME\tRATE\tHOURS"; print ""}{ print }' emp.dat
(printf_BEGIN1.bash)
END
The special pattern END matches after the last line of the last file has been processed
This program uses END to count the number of employees that have worked more than 15 hours
gawk '$3 > 15 { emp = emp + 1 }
END { print emp, "employees worked more than 15 hours" }' emp.dat
(printf_END1.bash)
Computing Sums and Averages
This program uses NR to compute average employee pay
gawk '{ pay = pay + $2 * $3 }
END { print NR, "employees"
printf( "total pay is $%7.2f\n", pay )
printf( "average pay is $%7.2f\n", pay/NR )}' emp.dat;
Question: What if NR == 0?
(printf_END2.bash)
Handling Text
This program finds which employee makes the most per hour
gawk '$2 > maxRate { maxRate = $2; maxEmp = $1 }
END { print "Highest hourly rate:", maxRate, "for", maxEmp )}' emp.dat;
(printf_END3.bash)
String Concatenation
This program new strings by combining old ones
gawk '{ names = names $1 " " }
END { print names }' emp.dat;
(printf_END4.bash)
Printing the Last Input Line
Although NR retains its value in an END action, $0 does not
This program is one solution to the problem
gawk '{ last = $0 }
END { print last }' emp.dat;
(printf_END5.bash)