1 / 21

Awk 1 – an introduction

Awk 1 – an introduction. Your scripts. I will put them on the shared drive. Different solutions – diversity Title of email (lab or question) Efficient? Clear? You will again insights Comments (what level) You do not need to reply to my auto reply. Coins.txt.

Download Presentation

Awk 1 – an introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Awk 1 – an introduction

  2. Your scripts • I will put them on the shared drive. • Different solutions – diversity • Title of email (lab or question) • Efficient? Clear? • You will again insights • Comments (what level) • You do not need to reply to my auto reply

  3. Coins.txt • gold 1 1986 USA American Eagle • gold 1 1908 Austria-Hungary Franz Josef 100 Korona • silver 10 1981 USA ingot • gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand

  4. Using awk • I could then invoke Awk to list all the gold pieces as follows: • awk '/gold/' coins.txt

  5. Selecting Fields • awk '/gold/ {print $5,$6,$7,$8}' coins.txt • This yields: • (ie. Prints out certain columns) • American Eagle Franz Josef 100 Korona ingot Krugerrand

  6. General Form • This example demonstrates the simplest general form of an Awk program: • awk <search pattern> {<program actions>}

  7. Fields • {print $5,$6,$7,$8}. • The "$5", "$6", "$7", and "$8" are "fields", or "field variables", which store the words in each line of text by their numeric sequence. "$1", for example, stores the first word in the line, "$2" has the second, and so on. By default, a "word" is defined as any string of printing characters separated by spaces.

  8. default program action • * Awk's default program action is to print the entire line, which is what "print" does when invoked without parameters. This means that the first example: • awk '/gold/' coins.txt • -- is the same as: • awk '/gold/ {print}' coins.txt

  9. $0 • Note that Awk recognizes the field variable $0 as representing the entire line, so this could also be written as: • awk '/gold/ {print $0}'

  10. If statement • I want to list all the coins that were minted before 1980. I invoke Awk as follows: • awk '{if ($3 < 1980) print $3, " ",$5,$6,$7,$8}' coins.txt • This yields: • 1908 Franz Josef 100 Korona • 1979 Krugerrand

  11. NR (number of records) • The next example prints out how many coins are in the collection: • awk 'END {print NR,"coins"}' coins.txt • This yields: • 13 coins

  12. general form of an Awk program • the general form of an Awk program to: • awk 'BEGIN {<initializations>} • <search pattern 1> {<program actions>} <search pattern 2> {<program actions>} ... END {<final actions>}'

  13. * Suppose the current price of gold is $425, and I want to figure out the approximate total value of the gold pieces in the coin collection. I invoke Awk as follows: • awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt • This yields: (note ounces is user defined) • value = $2592.5

  14. So the program action: • {ounces += $2} • Another way of saying: • {ounces = ounces + $2} • The final action is to compute and print the value of the gold: • END {print "value = $" 425*ounces}

  15. AWK PROGRAM EXAMPLE • Instead of doing it all from the command line • We can do it all from a file, • With the following syntax • awk -f <awk program file name>

  16. Output of program • Summary Data for Coin Collection: • Gold pieces: nn • Weight of gold pieces: nn.nn • Value of gold pieces: n,nnn.nn • Silver pieces: nn • Weight of silver pieces: nn.nn • Value of silver pieces: n,nnn.nn • Total number of pieces: nn • Value of collection: n,nnn.nn

  17. # This is an awk program that summarizes a gold coin collection. • /gold/ { num_gold++; wt_gold += $2 } # Get weight of gold. • END { val_gold = 485 * wt_gold; # Compute value of gold. • 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 ("\n"); printf (" Total number of pieces: %2d\n", NR); • printf (" Value of collection: %7.2f\n", total); }

  18. a few interesting features: • Comments can be inserted in the program by preceding them with a "#". • Note the statements "num_gold++" and "num_silver++". the "++" operator simply increments the specified variable by one. • Multiple statements can be written on the same line by separating them with a semicolon (";").

  19. Printf (print format) • printf("<format_code>",<parameters>) • For example, the format code "%2d" tells Awk to print a two-digit integer number, and the format code "%7.2f" tells Awk to print a seven-digit floating-point number, with two digits to the right of the decimal point.

  20. tutorial • http://www.vectorsite.net/tsawk_1.html#m1 • You can cut and paste the commands from here or from these slides. • You can easily look at this in your 2 hours private study time, and do not forget to mail me your scripts so far. ….. 

More Related