Substitute Array Elements into Regular Expression Statements / Variable Names
Been wondering if this is doable in AWK for some time but always worked around it in the past.
Below I initi开发者_运维问答alize an array with 3 months of the year... for readability I ommited the other 9 months. These months are then used in the if-statement as part of a regular expression, but AWK doesn't like it. I can't seem to find anything in the awk/gawk manuals regarding this kind of semantic... am I really stuck with restating the same code 12 times? Also is it possible to use arr[i] in the loop as a substring of a variable name? I wrote pseudo-code below to give an idea of what I'm trying to accomplish. I know it's doable in SNOBOL ;-) Thanks!
BEGIN {
arr[0] = "AUG"
arr[1] = "SEP"
arr[2] = "OCT"
}
{
for(i in arr)
{
if($1 ~ /arr[i]/)
{
#Controls flows into here if $1 matches AUG, SEP, OCT
#Furthermore, pretend I want to intialize a variable like AUGseen:
arr[i]seen = 1
}
}
}
If either of these things are doable I greatly appreciate pointers!
You can use match
for dynamic regex'es.
if(match($1, arr[i]))
I don't think that awk supports this concept, but using a function will be just as efficient:
# fail is the default return code, the user should insure that it does not
# exist as a key to arr
function amatch(candidate, arr, fail) {
for (i in arr)
if ( match(candidate,array[i]) ) return i;
return fail;
}
here's an alternative, it doesn't use arrays. (you can keep the array for other purposes)
BEGIN {
dates="AUG|SEP|OCT"
}
{
if( $1 ~ dates) {
print ...
}
}
精彩评论