| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Awk</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="UP" |
| 14 | TITLE="A Sed and Awk Micro-Primer" |
| 15 | HREF="sedawk.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="A Sed and Awk Micro-Primer" |
| 18 | HREF="sedawk.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Parsing and Managing Pathnames" |
| 21 | HREF="pathmanagement.html"><META |
| 22 | HTTP-EQUIV="Content-Style-Type" |
| 23 | CONTENT="text/css"><LINK |
| 24 | REL="stylesheet" |
| 25 | HREF="common/kde-common.css" |
| 26 | TYPE="text/css"><META |
| 27 | HTTP-EQUIV="Content-Type" |
| 28 | CONTENT="text/html; charset=iso-8859-1"><META |
| 29 | HTTP-EQUIV="Content-Language" |
| 30 | CONTENT="en"><LINK |
| 31 | REL="stylesheet" |
| 32 | HREF="common/kde-localised.css" |
| 33 | TYPE="text/css" |
| 34 | TITLE="KDE-English"><LINK |
| 35 | REL="stylesheet" |
| 36 | HREF="common/kde-default.css" |
| 37 | TYPE="text/css" |
| 38 | TITLE="KDE-Default"></HEAD |
| 39 | ><BODY |
| 40 | CLASS="SECT1" |
| 41 | BGCOLOR="#FFFFFF" |
| 42 | TEXT="#000000" |
| 43 | LINK="#AA0000" |
| 44 | VLINK="#AA0055" |
| 45 | ALINK="#AA0000" |
| 46 | STYLE="font-family: sans-serif;" |
| 47 | ><DIV |
| 48 | CLASS="NAVHEADER" |
| 49 | ><TABLE |
| 50 | SUMMARY="Header navigation table" |
| 51 | WIDTH="100%" |
| 52 | BORDER="0" |
| 53 | CELLPADDING="0" |
| 54 | CELLSPACING="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | COLSPAN="3" |
| 58 | ALIGN="center" |
| 59 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 60 | ></TR |
| 61 | ><TR |
| 62 | ><TD |
| 63 | WIDTH="10%" |
| 64 | ALIGN="left" |
| 65 | VALIGN="bottom" |
| 66 | ><A |
| 67 | HREF="sedawk.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | >Appendix C. A Sed and Awk Micro-Primer</TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="pathmanagement.html" |
| 82 | ACCESSKEY="N" |
| 83 | >Next</A |
| 84 | ></TD |
| 85 | ></TR |
| 86 | ></TABLE |
| 87 | ><HR |
| 88 | ALIGN="LEFT" |
| 89 | WIDTH="100%"></DIV |
| 90 | ><DIV |
| 91 | CLASS="SECT1" |
| 92 | ><H1 |
| 93 | CLASS="SECT1" |
| 94 | ><A |
| 95 | NAME="AWK" |
| 96 | ></A |
| 97 | >C.2. Awk</H1 |
| 98 | ><P |
| 99 | ><A |
| 100 | NAME="AWKREF" |
| 101 | ></A |
| 102 | ></P |
| 103 | ><P |
| 104 | ><I |
| 105 | CLASS="FIRSTTERM" |
| 106 | >Awk</I |
| 107 | > |
| 108 | <A |
| 109 | NAME="AEN23443" |
| 110 | HREF="#FTN.AEN23443" |
| 111 | >[1]</A |
| 112 | > |
| 113 | is a full-featured text processing language with a syntax |
| 114 | reminiscent of <I |
| 115 | CLASS="FIRSTTERM" |
| 116 | >C</I |
| 117 | >. While it possesses an |
| 118 | extensive set of operators and capabilities, we will cover only |
| 119 | a few of these here - the ones most useful in shell scripts.</P |
| 120 | ><P |
| 121 | >Awk breaks each line of input passed to it into |
| 122 | <A |
| 123 | NAME="FIELDREF2" |
| 124 | ></A |
| 125 | > |
| 126 | <A |
| 127 | HREF="special-chars.html#FIELDREF" |
| 128 | >fields</A |
| 129 | >. By default, a field |
| 130 | is a string of consecutive characters delimited by <A |
| 131 | HREF="special-chars.html#WHITESPACEREF" |
| 132 | >whitespace</A |
| 133 | >, though there are options |
| 134 | for changing this. Awk parses and operates on each separate |
| 135 | field. This makes it ideal for handling structured text files |
| 136 | -- especially tables -- data organized into consistent chunks, |
| 137 | such as rows and columns.</P |
| 138 | ><P |
| 139 | ><A |
| 140 | HREF="variables.html#SNGLQUO" |
| 141 | >Strong quoting</A |
| 142 | > and <A |
| 143 | HREF="special-chars.html#CODEBLOCKREF" |
| 144 | >curly brackets</A |
| 145 | > enclose blocks of |
| 146 | awk code within a shell script.</P |
| 147 | ><P |
| 148 | ><TABLE |
| 149 | BORDER="0" |
| 150 | BGCOLOR="#E0E0E0" |
| 151 | WIDTH="100%" |
| 152 | ><TR |
| 153 | ><TD |
| 154 | ><PRE |
| 155 | CLASS="PROGRAMLISTING" |
| 156 | > 1 # $1 is field #1, $2 is field #2, etc. |
| 157 | 2 |
| 158 | 3 echo one two | awk '{print $1}' |
| 159 | 4 # one |
| 160 | 5 |
| 161 | 6 echo one two | awk '{print $2}' |
| 162 | 7 # two |
| 163 | 8 |
| 164 | 9 # But what is field #0 ($0)? |
| 165 | 10 echo one two | awk '{print $0}' |
| 166 | 11 # one two |
| 167 | 12 # All the fields! |
| 168 | 13 |
| 169 | 14 |
| 170 | 15 awk '{print $3}' $filename |
| 171 | 16 # Prints field #3 of file $filename to stdout. |
| 172 | 17 |
| 173 | 18 awk '{print $1 $5 $6}' $filename |
| 174 | 19 # Prints fields #1, #5, and #6 of file $filename. |
| 175 | 20 |
| 176 | 21 awk '{print $0}' $filename |
| 177 | 22 # Prints the entire file! |
| 178 | 23 # Same effect as: cat $filename . . . or . . . sed '' $filename</PRE |
| 179 | ></TD |
| 180 | ></TR |
| 181 | ></TABLE |
| 182 | ></P |
| 183 | ><P |
| 184 | >We have just seen the awk <I |
| 185 | CLASS="FIRSTTERM" |
| 186 | >print</I |
| 187 | > command |
| 188 | in action. The only other feature of awk we need to deal with |
| 189 | here is variables. Awk handles variables similarly to shell |
| 190 | scripts, though a bit more flexibly.</P |
| 191 | ><P |
| 192 | ><TABLE |
| 193 | BORDER="0" |
| 194 | BGCOLOR="#E0E0E0" |
| 195 | WIDTH="100%" |
| 196 | ><TR |
| 197 | ><TD |
| 198 | ><PRE |
| 199 | CLASS="PROGRAMLISTING" |
| 200 | > 1 { total += ${column_number} }</PRE |
| 201 | ></TD |
| 202 | ></TR |
| 203 | ></TABLE |
| 204 | > |
| 205 | This adds the value of <TT |
| 206 | CLASS="PARAMETER" |
| 207 | ><I |
| 208 | >column_number</I |
| 209 | ></TT |
| 210 | > to |
| 211 | the running total of <TT |
| 212 | CLASS="PARAMETER" |
| 213 | ><I |
| 214 | >total</I |
| 215 | ></TT |
| 216 | >>. Finally, to print |
| 217 | <SPAN |
| 218 | CLASS="QUOTE" |
| 219 | >"total"</SPAN |
| 220 | >, there is an <B |
| 221 | CLASS="COMMAND" |
| 222 | >END</B |
| 223 | > command |
| 224 | block, executed after the script has processed all its input. |
| 225 | <TABLE |
| 226 | BORDER="0" |
| 227 | BGCOLOR="#E0E0E0" |
| 228 | WIDTH="100%" |
| 229 | ><TR |
| 230 | ><TD |
| 231 | ><PRE |
| 232 | CLASS="PROGRAMLISTING" |
| 233 | > 1 END { print total }</PRE |
| 234 | ></TD |
| 235 | ></TR |
| 236 | ></TABLE |
| 237 | ></P |
| 238 | ><P |
| 239 | >Corresponding to the <B |
| 240 | CLASS="COMMAND" |
| 241 | >END</B |
| 242 | >, there is a |
| 243 | <B |
| 244 | CLASS="COMMAND" |
| 245 | >BEGIN</B |
| 246 | >, for a code block to be performed before awk |
| 247 | starts processing its input.</P |
| 248 | ><P |
| 249 | >The following example illustrates how <B |
| 250 | CLASS="COMMAND" |
| 251 | >awk</B |
| 252 | > can |
| 253 | add text-parsing tools to a shell script.</P |
| 254 | ><DIV |
| 255 | CLASS="EXAMPLE" |
| 256 | ><HR><A |
| 257 | NAME="LETTERCOUNT2" |
| 258 | ></A |
| 259 | ><P |
| 260 | ><B |
| 261 | >Example C-1. Counting Letter Occurrences</B |
| 262 | ></P |
| 263 | ><TABLE |
| 264 | BORDER="0" |
| 265 | BGCOLOR="#E0E0E0" |
| 266 | WIDTH="100%" |
| 267 | ><TR |
| 268 | ><TD |
| 269 | ><PRE |
| 270 | CLASS="PROGRAMLISTING" |
| 271 | > 1 #! /bin/sh |
| 272 | 2 # letter-count2.sh: Counting letter occurrences in a text file. |
| 273 | 3 # |
| 274 | 4 # Script by nyal [[email protected]]. |
| 275 | 5 # Used in ABS Guide with permission. |
| 276 | 6 # Recommented and reformatted by ABS Guide author. |
| 277 | 7 # Version 1.1: Modified to work with gawk 3.1.3. |
| 278 | 8 # (Will still work with earlier versions.) |
| 279 | 9 |
| 280 | 10 |
| 281 | 11 INIT_TAB_AWK="" |
| 282 | 12 # Parameter to initialize awk script. |
| 283 | 13 count_case=0 |
| 284 | 14 FILE_PARSE=$1 |
| 285 | 15 |
| 286 | 16 E_PARAMERR=85 |
| 287 | 17 |
| 288 | 18 usage() |
| 289 | 19 { |
| 290 | 20 echo "Usage: letter-count.sh file letters" 2>&1 |
| 291 | 21 # For example: ./letter-count2.sh filename.txt a b c |
| 292 | 22 exit $E_PARAMERR # Too few arguments passed to script. |
| 293 | 23 } |
| 294 | 24 |
| 295 | 25 if [ ! -f "$1" ] ; then |
| 296 | 26 echo "$1: No such file." 2>&1 |
| 297 | 27 usage # Print usage message and exit. |
| 298 | 28 fi |
| 299 | 29 |
| 300 | 30 if [ -z "$2" ] ; then |
| 301 | 31 echo "$2: No letters specified." 2>&1 |
| 302 | 32 usage |
| 303 | 33 fi |
| 304 | 34 |
| 305 | 35 shift # Letters specified. |
| 306 | 36 for letter in `echo $@` # For each one . . . |
| 307 | 37 do |
| 308 | 38 INIT_TAB_AWK="$INIT_TAB_AWK tab_search[${count_case}] = \ |
| 309 | 39 \"$letter\"; final_tab[${count_case}] = 0; " |
| 310 | 40 # Pass as parameter to awk script below. |
| 311 | 41 count_case=`expr $count_case + 1` |
| 312 | 42 done |
| 313 | 43 |
| 314 | 44 # DEBUG: |
| 315 | 45 # echo $INIT_TAB_AWK; |
| 316 | 46 |
| 317 | 47 cat $FILE_PARSE | |
| 318 | 48 # Pipe the target file to the following awk script. |
| 319 | 49 |
| 320 | 50 # --------------------------------------------------------------------- |
| 321 | 51 # Earlier version of script: |
| 322 | 52 # awk -v tab_search=0 -v final_tab=0 -v tab=0 -v \ |
| 323 | 53 # nb_letter=0 -v chara=0 -v chara2=0 \ |
| 324 | 54 |
| 325 | 55 awk \ |
| 326 | 56 "BEGIN { $INIT_TAB_AWK } \ |
| 327 | 57 { split(\$0, tab, \"\"); \ |
| 328 | 58 for (chara in tab) \ |
| 329 | 59 { for (chara2 in tab_search) \ |
| 330 | 60 { if (tab_search[chara2] == tab[chara]) { final_tab[chara2]++ } } } } \ |
| 331 | 61 END { for (chara in final_tab) \ |
| 332 | 62 { print tab_search[chara] \" => \" final_tab[chara] } }" |
| 333 | 63 # --------------------------------------------------------------------- |
| 334 | 64 # Nothing all that complicated, just . . . |
| 335 | 65 #+ for-loops, if-tests, and a couple of specialized functions. |
| 336 | 66 |
| 337 | 67 exit $? |
| 338 | 68 |
| 339 | 69 # Compare this script to letter-count.sh.</PRE |
| 340 | ></TD |
| 341 | ></TR |
| 342 | ></TABLE |
| 343 | ><HR></DIV |
| 344 | ><P |
| 345 | >For simpler examples of awk within shell scripts, see: |
| 346 | <OL |
| 347 | TYPE="1" |
| 348 | ><LI |
| 349 | ><P |
| 350 | ><A |
| 351 | HREF="internal.html#EX44" |
| 352 | >Example 15-14</A |
| 353 | ></P |
| 354 | ></LI |
| 355 | ><LI |
| 356 | ><P |
| 357 | ><A |
| 358 | HREF="redircb.html#REDIR4" |
| 359 | >Example 20-8</A |
| 360 | ></P |
| 361 | ></LI |
| 362 | ><LI |
| 363 | ><P |
| 364 | ><A |
| 365 | HREF="filearchiv.html#STRIPC" |
| 366 | >Example 16-32</A |
| 367 | ></P |
| 368 | ></LI |
| 369 | ><LI |
| 370 | ><P |
| 371 | ><A |
| 372 | HREF="wrapper.html#COLTOTALER" |
| 373 | >Example 36-5</A |
| 374 | ></P |
| 375 | ></LI |
| 376 | ><LI |
| 377 | ><P |
| 378 | ><A |
| 379 | HREF="ivr.html#COLTOTALER2" |
| 380 | >Example 28-2</A |
| 381 | ></P |
| 382 | ></LI |
| 383 | ><LI |
| 384 | ><P |
| 385 | ><A |
| 386 | HREF="internal.html#COLTOTALER3" |
| 387 | >Example 15-20</A |
| 388 | ></P |
| 389 | ></LI |
| 390 | ><LI |
| 391 | ><P |
| 392 | ><A |
| 393 | HREF="procref1.html#PIDID" |
| 394 | >Example 29-3</A |
| 395 | ></P |
| 396 | ></LI |
| 397 | ><LI |
| 398 | ><P |
| 399 | ><A |
| 400 | HREF="procref1.html#CONSTAT" |
| 401 | >Example 29-4</A |
| 402 | ></P |
| 403 | ></LI |
| 404 | ><LI |
| 405 | ><P |
| 406 | ><A |
| 407 | HREF="loops.html#FILEINFO" |
| 408 | >Example 11-3</A |
| 409 | ></P |
| 410 | ></LI |
| 411 | ><LI |
| 412 | ><P |
| 413 | ><A |
| 414 | HREF="extmisc.html#BLOTOUT" |
| 415 | >Example 16-61</A |
| 416 | ></P |
| 417 | ></LI |
| 418 | ><LI |
| 419 | ><P |
| 420 | ><A |
| 421 | HREF="randomvar.html#SEEDINGRANDOM" |
| 422 | >Example 9-16</A |
| 423 | ></P |
| 424 | ></LI |
| 425 | ><LI |
| 426 | ><P |
| 427 | ><A |
| 428 | HREF="moreadv.html#IDELETE" |
| 429 | >Example 16-4</A |
| 430 | ></P |
| 431 | ></LI |
| 432 | ><LI |
| 433 | ><P |
| 434 | ><A |
| 435 | HREF="manipulatingvars.html#SUBSTRINGEX" |
| 436 | >Example 10-6</A |
| 437 | ></P |
| 438 | ></LI |
| 439 | ><LI |
| 440 | ><P |
| 441 | ><A |
| 442 | HREF="assortedtips.html#SUMPRODUCT" |
| 443 | >Example 36-19</A |
| 444 | ></P |
| 445 | ></LI |
| 446 | ><LI |
| 447 | ><P |
| 448 | ><A |
| 449 | HREF="loops.html#USERLIST" |
| 450 | >Example 11-9</A |
| 451 | ></P |
| 452 | ></LI |
| 453 | ><LI |
| 454 | ><P |
| 455 | ><A |
| 456 | HREF="wrapper.html#PRASC" |
| 457 | >Example 36-4</A |
| 458 | ></P |
| 459 | ></LI |
| 460 | ><LI |
| 461 | ><P |
| 462 | ><A |
| 463 | HREF="mathc.html#HYPOT" |
| 464 | >Example 16-53</A |
| 465 | ></P |
| 466 | ></LI |
| 467 | ><LI |
| 468 | ><P |
| 469 | ><A |
| 470 | HREF="asciitable.html#ASCII3SH" |
| 471 | >Example T-3</A |
| 472 | ></P |
| 473 | ></LI |
| 474 | ></OL |
| 475 | > |
| 476 | </P |
| 477 | ><P |
| 478 | >That's all the awk we'll cover here, folks, but there's lots |
| 479 | more to learn. See the appropriate references in the <A |
| 480 | HREF="biblio.html" |
| 481 | ><I |
| 482 | >Bibliography</I |
| 483 | ></A |
| 484 | >.</P |
| 485 | ></DIV |
| 486 | ><H3 |
| 487 | CLASS="FOOTNOTES" |
| 488 | >Notes</H3 |
| 489 | ><TABLE |
| 490 | BORDER="0" |
| 491 | CLASS="FOOTNOTES" |
| 492 | WIDTH="100%" |
| 493 | ><TR |
| 494 | ><TD |
| 495 | ALIGN="LEFT" |
| 496 | VALIGN="TOP" |
| 497 | WIDTH="5%" |
| 498 | ><A |
| 499 | NAME="FTN.AEN23443" |
| 500 | HREF="awk.html#AEN23443" |
| 501 | >[1]</A |
| 502 | ></TD |
| 503 | ><TD |
| 504 | ALIGN="LEFT" |
| 505 | VALIGN="TOP" |
| 506 | WIDTH="95%" |
| 507 | ><P |
| 508 | >Its name derives from the initials of its authors, |
| 509 | <B |
| 510 | CLASS="COMMAND" |
| 511 | >A</B |
| 512 | >ho, <B |
| 513 | CLASS="COMMAND" |
| 514 | >W</B |
| 515 | >einberg, and |
| 516 | <B |
| 517 | CLASS="COMMAND" |
| 518 | >K</B |
| 519 | >ernighan.</P |
| 520 | ></TD |
| 521 | ></TR |
| 522 | ></TABLE |
| 523 | ><DIV |
| 524 | CLASS="NAVFOOTER" |
| 525 | ><HR |
| 526 | ALIGN="LEFT" |
| 527 | WIDTH="100%"><TABLE |
| 528 | SUMMARY="Footer navigation table" |
| 529 | WIDTH="100%" |
| 530 | BORDER="0" |
| 531 | CELLPADDING="0" |
| 532 | CELLSPACING="0" |
| 533 | ><TR |
| 534 | ><TD |
| 535 | WIDTH="33%" |
| 536 | ALIGN="left" |
| 537 | VALIGN="top" |
| 538 | ><A |
| 539 | HREF="sedawk.html" |
| 540 | ACCESSKEY="P" |
| 541 | >Prev</A |
| 542 | ></TD |
| 543 | ><TD |
| 544 | WIDTH="34%" |
| 545 | ALIGN="center" |
| 546 | VALIGN="top" |
| 547 | ><A |
| 548 | HREF="index.html" |
| 549 | ACCESSKEY="H" |
| 550 | >Home</A |
| 551 | ></TD |
| 552 | ><TD |
| 553 | WIDTH="33%" |
| 554 | ALIGN="right" |
| 555 | VALIGN="top" |
| 556 | ><A |
| 557 | HREF="pathmanagement.html" |
| 558 | ACCESSKEY="N" |
| 559 | >Next</A |
| 560 | ></TD |
| 561 | ></TR |
| 562 | ><TR |
| 563 | ><TD |
| 564 | WIDTH="33%" |
| 565 | ALIGN="left" |
| 566 | VALIGN="top" |
| 567 | >A Sed and Awk Micro-Primer</TD |
| 568 | ><TD |
| 569 | WIDTH="34%" |
| 570 | ALIGN="center" |
| 571 | VALIGN="top" |
| 572 | ><A |
| 573 | HREF="sedawk.html" |
| 574 | ACCESSKEY="U" |
| 575 | >Up</A |
| 576 | ></TD |
| 577 | ><TD |
| 578 | WIDTH="33%" |
| 579 | ALIGN="right" |
| 580 | VALIGN="top" |
| 581 | >Parsing and Managing Pathnames</TD |
| 582 | ></TR |
| 583 | ></TABLE |
| 584 | ></DIV |
| 585 | ></BODY |
| 586 | ></HTML |
| 587 | > |