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.

Chapter 13: Practical awk Programs 247in the original line. For each two empty elements (‘@@’ in the original file), we have to adda single ‘@’ symbol back in.When the processing of the array is finished, join is called with the value of SUBSEP, torejoin the pieces back into a single line. That line is then printed to the output file:/^@c(omment)?[ \t]+file/ \{if (NF != 3) {e = (FILENAME ":" FNR ": badly formed ‘file’ line")print e > "/dev/stderr"next}if ($3 != curfile) {if (curfile != "")close(curfile)curfile = $3}}for (;;) {if ((getline line) curfilecontinue}n = split(line, a, "@")# if a[1] == "", means leading @,# don’t add one back in.for (i = 2; i curfile}An important thing to note is the use of the ‘>’ redirection. Output done with ‘>’only opens the file once; it stays open and subsequent output is appended to the file (seeSection 4.6 [Redirecting Output of print and printf], page 66). This makes it easy to

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

Saved successfully!

Ooh no, something went wrong!