--- l-awk1.xml-old 2005-08-20 17:55:32.000000000 +0200 +++ l-awk1.xml 2005-08-20 18:08:37.000000000 +0200 @@ -51,13 +51,15 @@

-Sure, awk doesn't have a great name. But it is a great language. Awk is geared -toward text processing and report generation, yet features many well-designed -features that allow for serious programming. And, unlike some languages, awk's -syntax is familiar, and borrows some of the best parts of languages like C, -python, and bash (although, technically, awk was created before both python and -bash). Awk is one of those languages that, once learned, will become a key part -of your strategic coding arsenal. +Sure, awk doesn't have a great name. But it is a great language. Awk stands for +"Aho, Weinberger, Kernighan" - three of the most famed computer scientists, and +the creators of the language. Awk is geared toward text processing and report +generation, yet features many well-designed features that allow for serious +programming. And, unlike some languages, awk's syntax is familiar, and borrows +some of the best parts of languages like C, python, and bash (although, +technically, awk was created before both python and bash). Awk is one of those +languages that, once learned, will become a key part of your strategic coding +arsenal.

@@ -66,6 +68,10 @@ The first awk +
+$ awk '{ print }' /etc/passwd
+
+

You should see the contents of your /etc/passwd file appear before your eyes. Now, for an explanation of what awk did. When we called awk, we @@ -139,6 +145,10 @@ { print $1 } +

+$ awk -f script.awk /etc/passwd
+
+

The difference between these two methods has to do with how we set the field separator. In this script, the field separator is specified within the code @@ -204,7 +214,11 @@ Conditional statements -

+
+$5 ~ /root/ { print $3 }
+
+ +
 { 
     if ( $5 ~ /root/ ) { 
         print $3 
@@ -269,6 +283,12 @@
 Numeric variables!
 
 
+
+BEGIN { x = 0 }
+/^$/ { x = x + 1 }
+END { print x }
+
+

In the BEGIN block, we initialize our integer variable x to zero. Then, each time awk encounters a blank line, awk will execute the x=x+1 statement,