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 229It is also possible to write the loop this way:for (i in copy)if (append)print >> copy[i]elseprint > copy[i]This is more concise but it is also less efficient. The ‘if’ is tested for each record and foreach output file. By duplicating the loop body, the ‘if’ is only tested once for each inputrecord. If there are N input records and M output files, the first method only executes N‘if’ statements, while the second executes N*M ‘if’ statements.Finally, the END rule cleans up by closing all the output files:END \{for (i in copy)close(copy[i])}13.2.6 Printing Nonduplicated Lines of TextThe uniq utility reads sorted lines of data on its standard input, and by default removesduplicate lines. In other words, it only prints unique lines—hence the name. uniq has anumber of options. The usage is as follows:uniq [-udc [-n]] [+n] [ input file [ output file ]]The options for uniq are:-d Pnly print only repeated lines.-u Print only nonrepeated lines.-c Count lines. This option overrides ‘-d’ and ‘-u’. Both repeated and nonrepeatedlines are counted.-n Skip n fields before comparing lines. The definition of fields is similar to awk’sdefault: nonwhitespace characters separated by runs of spaces and/or TABs.+n Skip n characters before comparing lines. Any fields specified with ‘-n’ areskipped first.input fileData is read from the input file named on the command line, instead of fromthe standard input.output fileThe generated output is sent to the named output file, instead of to the standardoutput.Normally uniq behaves as if both the ‘-d’ and ‘-u’ options are provided.uniq uses the getopt library function (see Section 12.4 [Processing Command-Line Options],page 201) and the join library function (see Section 12.2.7 [Merging an Array intoa String], page 195).

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

Saved successfully!

Ooh no, something went wrong!