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 8: Functions 135awk prints:but not very quicklyFIND MelvinJF+KMThis line is property of Reality Engineering Co.Melvin was here.Match of ru+n found at 12 in My program runsMatch of Melvin found at 1 in Melvin was here.If array is present, it is cleared, and then the 0th element of array is set tothe entire portion of string matched by regexp. If regexp contains parentheses,the integer-indexed elements of array are set to contain the portion of stringmatching the corresponding parenthesized subexpression. For example:$ echo foooobazbarrrrr |> gawk ’{ match($0, /(fo+).+(bar*)/, arr)> print arr[1], arr[2] }’⊣ foooo barrrrrIn addition, beginning with gawk 3.1.2, multidimensional subscripts are availableproviding the start index and length of each matched subexpression:$ echo foooobazbarrrrr |> gawk ’{ match($0, /(fo+).+(bar*)/, arr)> print arr[1], arr[2]> print arr[1, "start"], arr[1, "length"]> print arr[2, "start"], arr[2, "length"]> }’⊣ foooo barrrrr⊣ 1 5⊣ 9 7There may not be subscripts for the start and index for every parenthesizedsubexpressions, since they may not all have matched text; thus they should betested for with the in operator (see Section 7.2 [Referring to an Array Element],page 120).The array argument to match is a gawk extension. In compatibility mode (seeSection 11.2 [Command-Line Options], page 177), using a third argument is afatal error.split(string, array [, fieldsep])This function divides string into pieces separated by fieldsep and stores thepieces in array. The first piece is stored in array[1], the second piece inarray[2], and so forth. The string value of the third argument, fieldsep, is aregexp describing where to split string (much as FS can be a regexp describingwhere to split input records). If fieldsep is omitted, the value of FS is used.split returns the number of elements created.The split function splits strings into pieces in a manner similar to the wayinput lines are split into fields. For example:split("cul-de-sac", a, "-")

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

Saved successfully!

Ooh no, something went wrong!