Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting | ||
---|---|---|
Prev | Appendix C. A Sed and Awk Micro-Primer | Next |
Awk is a full-featured text processing language with a syntax reminiscent of C. While it possesses an extensive set of operators and capabilities, we will cover only a couple of these here - the ones most useful for shell scripting.
Awk breaks each line of input passed to it into fields. By default, a field is a string of consecutive characters separated by whitespace, though there are options for changing the delimiter. Awk parses and operates on each separate field. This makes awk ideal for handling structured text files -- especially tables -- data organized into consistent chunks, such as rows and columns.
Strong quoting (single quotes) and curly brackets enclose segments of awk code within a shell script.
1 awk '{print $3}' $filename 2 # Prints field #3 of file $filename to stdout. 3 4 awk '{print $1 $5 $6}' $filename 5 # Prints fields #1, #5, and #6 of file $filename. |
We have just seen the awk print command in action. The only other feature of awk we need to deal with here is variables. Awk handles variables similarly to shell scripts, though a bit more flexibly.
1 { total += ${column_number} } |
1 END { print total } |
Corresponding to the END, there is a BEGIN, for a code block to be performed before awk starts processing its input.
The following example illustrates how awk can add text-parsing tools to a shell script.
Example C-1. Counting Letter Occurrences
1 #! /bin/sh 2 # letter-count.sh: Counting letter occurrences in a text file. 3 # 4 # Script by nyal (nyal@voila.fr). 5 # Used with permission. 6 # Recommented by document author. 7 8 9 INIT_TAB_AWK="" 10 # Parameter to initialize awk script. 11 count_case=0 12 FILE_PARSE=$1 13 14 E_PARAMERR=65 15 16 usage() 17 { 18 echo "Usage: letter-count.sh file letters" 2>&1 19 # For example: ./letter-count.sh filename.txt a b c 20 exit $E_PARAMERR # Not enough arguments passed to script. 21 } 22 23 if [ ! -f "$1" ] ; then 24 echo "$1: No such file." 2>&1 25 usage # Print usage message and exit. 26 fi 27 28 if [ -z "$2" ] ; then 29 echo "$2: No letters specified." 2>&1 30 usage 31 fi 32 33 shift # Letters specified. 34 for letter in `echo $@` # For each one . . . 35 do 36 INIT_TAB_AWK="$INIT_TAB_AWK tab_search[${count_case}] = \"$letter\"; final_tab[${count_case}] = 0; " 37 # Pass as parameter to awk script below. 38 count_case=`expr $count_case + 1` 39 done 40 41 # DEBUG: 42 # echo $INIT_TAB_AWK; 43 44 cat $FILE_PARSE | 45 # Pipe the target file to the following awk script. 46 47 # -------------------------------------------------------------------------------- 48 awk -v tab_search=0 -v final_tab=0 -v tab=0 -v nb_letter=0 -v chara=0 -v chara2=0 \ 49 "BEGIN { $INIT_TAB_AWK } \ 50 { split(\$0, tab, \"\"); \ 51 for (chara in tab) \ 52 { for (chara2 in tab_search) \ 53 { if (tab_search[chara2] == tab[chara]) { final_tab[chara2]++ } } } } \ 54 END { for (chara in final_tab) \ 55 { print tab_search[chara] \" => \" final_tab[chara] } }" 56 # -------------------------------------------------------------------------------- 57 # Nothing all that complicated, just . . . 58 #+ for-loops, if-tests, and a couple of specialized functions. 59 60 exit $? |
For simpler examples of awk within shell scripts, see:
That's all the awk we'll cover here, folks, but there's lots more to learn. See the appropriate references in the Bibliography.