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 219function set_charlist( field, i, j, f, g, t,filler, last, len){field = 1 # count total fieldsn = split(fieldlist, f, ",")j = 1 # index in flistfor (i = 1; i = g[2]) {printf("bad character list: %s\n",f[i]) > "/dev/stderr"exit 1}len = g[2] - g[1] + 1if (g[1] > 1) # compute length of fillerfiller = g[1] - last - 1elsefiller = 0if (filler)t[field++] = filler}t[field++] = lenlast = g[2]flist[j++] = field - 1} else {if (f[i] > 1)filler = f[i] - last - 1elsefiller = 0if (filler)t[field++] = fillert[field++] = 1last = f[i]flist[j++] = field - 1}# length of field}FIELDWIDTHS = join(t, 1, field - 1)nfields = j - 1Next is the rule that actually processes the data. If the ‘-s’ option is given, thensuppress is true. The first if statement makes sure that the input record does have thefield separator. If cut is processing fields, suppress is true, and the field separator characteris not in the record, then the record is skipped.If the record is valid, then gawk has split the data into fields, either using the characterin FS or using fixed-length fields and FIELDWIDTHS. The loop goes through the list of fields

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

Saved successfully!

Ooh no, something went wrong!