21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

14 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>passes it to awk. Doing this leads to confusing behavior—most likely a usage diagnostic ofsome sort from awk.Finally, the value of ARGV[0] (see Section 6.5 [Built-in Variables], page 110) varies dependingupon your operating system. Some systems put ‘awk’ there, some put the fullpathname of awk (such as ‘/bin/awk’), and some put the name of your script (‘advice’).Don’t rely on the value of ARGV[0] to provide your script name.1.1.5 Comments in awk ProgramsA comment is some text that is included in a program for the sake of human readers; itis not really an executable part of the program. Comments can explain what the programdoes and how it works. Nearly all programming languages have provisions for comments,as programs are typically hard to understand without them.In the awk language, a comment starts with the sharp sign character (‘#’) and continuesto the end of the line. The ‘#’ does not have to be the first character on the line. The awklanguage ignores the rest of a line following a sharp sign. For example, we could have putthe following into ‘advice’:# This program prints a nice friendly message. It helps# keep novice users from being afraid of the computer.BEGIN { print "Don’t Panic!" }You can put comment lines into keyboard-composed throwaway awk programs, but thisusually isn’t very useful; the purpose of a comment is to help you or another person understandthe program when reading it at a later time.Caution: As mentioned in Section 1.1.1 [One-Shot Throwaway awk Programs], page 11,you can enclose small to medium programs in single quotes, in order to keep your shellscripts self-contained. When doing so, don’t put an apostrophe (i.e., a single quote) intoa comment (or anywhere else in your program). The shell interprets the quote as theclosing quote for the entire program. As a result, usually the shell prints a message aboutmismatched quotes, and if awk actually runs, it will probably print strange messages aboutsyntax errors. For example, look at the following:$ awk ’{ print "hello" } # let’s be cute’>The shell sees that the first two quotes match, and that a new quoted object begins atthe end of the command line. It therefore prompts with the secondary prompt, waiting formore input. With Unix awk, closing the quoted string produces this result:$ awk ’{ print "hello" } # let’s be cute’> ’error awk: can’t open file beerror source line number 1Putting a backslash before the single quote in ‘let’s’ wouldn’t help, since backslashesare not special inside single quotes. The next subsection describes the shell’s quoting rules.1.1.6 Shell-Quoting IssuesFor short to medium length awk programs, it is most convenient to enter the program on theawk command line. This is best done by enclosing the entire program in single quotes. This

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

Saved successfully!

Ooh no, something went wrong!