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 227}i++ # skip data file nameif (i in ARGV) {outfile = ARGV[i]ARGV[i] = ""}s1 = s2 = "a"out = (outfile s1 s2)The next rule does most of the work. tcount (temporary count) tracks how many lineshave been printed to the output file so far. If it is greater than count, it is time to closethe current file and start a new one. s1 and s2 track the current suffixes for the file name.If they are both ‘z’, the file is just too big. Otherwise, s1 moves to the next letter in thealphabet and s2 starts over again at ‘a’:{}if (++tcount > count) {close(out)if (s2 == "z") {if (s1 == "z") {printf("split: %s is too large to split\n",FILENAME) > "/dev/stderr"exit 1}s1 = chr(ord(s1) + 1)s2 = "a"}elses2 = chr(ord(s2) + 1)out = (outfile s1 s2)tcount = 1}print > outThe usage function simply prints an error message and exits:function usage( e){e = "usage: split [-num] [file] [outname]"print e > "/dev/stderr"exit 1}The variable e is used so that the function fits nicely on the page.This program is a bit sloppy; it relies on awk to automatically close the last file insteadof doing it in an END rule. It also assumes that letters are contiguous in the character set,which isn’t true for EBCDIC systems.

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

Saved successfully!

Ooh no, something went wrong!