21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13: Practical awk Programs 223of matches is inverted using the ‘!’ operator. fcount is incremented with the value ofmatches, which is either one or zero, depending upon a successful or unsuccessful match.If the line does not match, the next statement just moves on to the next record.A number of additional tests are made, but they are only done if we are not countinglines. First, if the user only wants exit status (no_print is true), then it is enough toknow that one line in this file matched, and we can skip on to the next file with nextfile.Similarly, if we are only printing file names, we can print the file name, and then skip to thenext file with nextfile. Finally, each line is printed, with a leading file name and colon ifnecessary:{}matches = ($0 ~ pattern)if (invert)matches = ! matchesfcount += matches # 1 or 0if (! matches)nextif (! count_only) {if (no_print)nextfile}if (filenames_only) {print FILENAMEnextfile}if (do_filenames)print FILENAME ":" $0elseprintThe END rule takes care of producing the correct exit status. If there are no matches,the exit status is one; otherwise it is zero:END \{if (total == 0)exit 1exit 0}The usage function prints a usage message in case of invalid options, and then exits:function usage( e){

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!