Subject: another useful feature from GNU world: awk/match
To: None <tech-userlevel@NetBSD.org>
From: Aleksey Cheusov <cheusov@tut.by>
List: tech-userlevel
Date: 04/17/2006 11:25:20
In GNU awk there is good and useful extention, the optional
third parameter of 'match' function of GNU awk.
`match(STRING, REGEXP [, ARRAY])'
...
If ARRAY is present, it is cleared, and then the 0th element of
ARRAY is set to the entire portion of STRING matched by REGEXP.
If REGEXP contains parentheses, the integer-indexed elements of
ARRAY are set to contain the portion of STRING matching the
corresponding parenthesized subexpression. For example:
...
In addition, beginning with `gawk' 3.1.2, multidimensional
subscripts are available providing 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 7
--
Best regards, Aleksey Cheusov.