http://www.vectorsite.net/tsawk_1.html#m1
Use the following as a guide:
awk '/gold/ {print $5,$6,$7,$8}' coins.txt
print fields 5, 6, 7, 8 from coints.txt that have "gold" somewhere on the line.
awk '/gold/ {print $0}' coints.txt
print all lines from coins.txt that have "gold" somewhere on the line.
awk 'END {print NR,"coins"}' coins.txt
print the number of lines in coins.txt
awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt
print the sum of the 2nd column * 425 of all rows that have "gold" somewhere in coins.txt
The following is an awk program that is run bu invoking the following:
awk -f script.awk coins.txt
/gold/ { num_gold++; wt_gold += $2 } # Get weight of gold.
/silver/ { num_silver++; wt_silver += $2 } # Get weight of silver.
END { val_gold = 485 * wt_gold; # Compute value of gold.
val_silver = 16 * wt_silver; # Compute value of silver.
total = val_gold + val_silver;
print "Summary data for coin collection:"; # Print results.
printf ("\n");
printf (" Gold pieces: %2d\n", num_gold);
printf (" Weight of gold pieces: %5.2f\n", wt_gold);
printf (" Value of gold pieces: %7.2f\n",val_gold);
printf ("\n");
printf (" Silver pieces: %2d\n", num_silver);
printf (" Weight of silver pieces: %5.2f\n", wt_silver);
printf (" Value of silver pieces: %7.2f\n",val_silver);
printf ("\n");
printf (" Total number of pieces: %2d\n", NR);
printf (" Value of collection: %7.2f\n", total); }
WWSS
2 hours ago