Lines 51-63
Link Here
|
51 |
</p> |
51 |
</p> |
52 |
|
52 |
|
53 |
<p> |
53 |
<p> |
54 |
Sure, awk doesn't have a great name. But it is a great language. Awk is geared |
54 |
Sure, awk doesn't have a great name. But it is a great language. Awk stands for |
55 |
toward text processing and report generation, yet features many well-designed |
55 |
"Aho, Weinberger, Kernighan" - three of the most famed computer scientists, and |
56 |
features that allow for serious programming. And, unlike some languages, awk's |
56 |
the creators of the language. Awk is geared toward text processing and report |
57 |
syntax is familiar, and borrows some of the best parts of languages like C, |
57 |
generation, yet features many well-designed features that allow for serious |
58 |
python, and bash (although, technically, awk was created before both python and |
58 |
programming. And, unlike some languages, awk's syntax is familiar, and borrows |
59 |
bash). Awk is one of those languages that, once learned, will become a key part |
59 |
some of the best parts of languages like C, python, and bash (although, |
60 |
of your strategic coding arsenal. |
60 |
technically, awk was created before both python and bash). Awk is one of those |
|
|
61 |
languages that, once learned, will become a key part of your strategic coding |
62 |
arsenal. |
61 |
</p> |
63 |
</p> |
62 |
|
64 |
|
63 |
</body> |
65 |
</body> |
Lines 66-71
Link Here
|
66 |
<title>The first awk</title> |
68 |
<title>The first awk</title> |
67 |
<body> |
69 |
<body> |
68 |
|
70 |
|
|
|
71 |
<pre caption="First awk"> |
72 |
$ <i>awk '{ print }' /etc/passwd</i> |
73 |
</pre> |
74 |
|
69 |
<p> |
75 |
<p> |
70 |
You should see the contents of your <path>/etc/passwd</path> file appear before |
76 |
You should see the contents of your <path>/etc/passwd</path> file appear before |
71 |
your eyes. Now, for an explanation of what awk did. When we called awk, we |
77 |
your eyes. Now, for an explanation of what awk did. When we called awk, we |
Lines 139-144
Link Here
|
139 |
{ print $1 } |
145 |
{ print $1 } |
140 |
</pre> |
146 |
</pre> |
141 |
|
147 |
|
|
|
148 |
<pre caption="Executing the sample script"> |
149 |
$ <i>awk -f script.awk /etc/passwd</i> |
150 |
</pre> |
151 |
|
142 |
<p> |
152 |
<p> |
143 |
The difference between these two methods has to do with how we set the field |
153 |
The difference between these two methods has to do with how we set the field |
144 |
separator. In this script, the field separator is specified within the code |
154 |
separator. In this script, the field separator is specified within the code |
Lines 204-210
Link Here
|
204 |
<title>Conditional statements</title> |
214 |
<title>Conditional statements</title> |
205 |
<body> |
215 |
<body> |
206 |
|
216 |
|
207 |
<pre caption="if"> |
217 |
<pre caption="Extracting the root record with regexp"> |
|
|
218 |
$5 ~ /root/ { print $3 } |
219 |
</pre> |
220 |
|
221 |
<pre caption="Extracting the root record with if statement"> |
208 |
{ |
222 |
{ |
209 |
if ( $5 ~ /root/ ) { |
223 |
if ( $5 ~ /root/ ) { |
210 |
print $3 |
224 |
print $3 |
Lines 269-274
Link Here
|
269 |
<title>Numeric variables!</title> |
283 |
<title>Numeric variables!</title> |
270 |
<body> |
284 |
<body> |
271 |
|
285 |
|
|
|
286 |
<pre caption="Numeric variables!"> |
287 |
BEGIN { x = 0 } |
288 |
/^$/ { x = x + 1 } |
289 |
END { print x } |
290 |
</pre> |
291 |
|
272 |
<p> |
292 |
<p> |
273 |
In the BEGIN block, we initialize our integer variable x to zero. Then, each |
293 |
In the BEGIN block, we initialize our integer variable x to zero. Then, each |
274 |
time awk encounters a blank line, awk will execute the x=x+1 statement, |
294 |
time awk encounters a blank line, awk will execute the x=x+1 statement, |