NAWK - looping with match() function

i'm trying to use the "before" output from the match() function as part of the results of each Regex match... but...

My input data: (from an input file)

i only show the first record in my file.. all other records are similar.

mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql $AI_SQL/wkly_inqry_trtry.sql $AI_SQL/wkly_nb_trtry.sql \

 
#!/usr/bin/nawk -f
 
BEGIN {
pat="AI_SQL\/[a-zA-Z_]+\.sql"
}
 
{
    while (match($0, pat)) {
       before = substr($0,1,RSTART-1);
       pattern = substr($0,RSTART,RLENGTH);
       printf("%s%s\n", before, pattern);
       $0=substr($0, RSTART+RLENGTH)
    }
}

I was expecting to see the "before" in my printf() literaly before "pattern" each time i cycle through the while loop, but i'm only getting it the first time it loops through.. :confused:

Results i'm getting for above code:

mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql
$AI_SQL/wkly_inqry_trtry.sql
$AI_SQL/wkly_nb_trtry.sql

Results i'm ultimately trying to generate:

mds_ar/bin/uedw92wp.ksh:$AI_SQL/wkly_inqry.sql
mds_ar/bin/uedw92wp.ksh:$Ai_SQL/wkly_inqry_trtry.sql
mds_ar/bin/uedw92wp.ksh:$AI_SQL/wkly_nb_trtry.sql

no, because you're overwriting the the whole record/line on every iteration through the 'while' loop - chopping it up to 'nothing' with 'match' and the '$0=...' assignment.

An alternative - holding on to the FIRST 'before' pattern and printing it out for every 'match':

#!/usr/bin/nawk -f

BEGIN {
pat="AI_SQL\/[a-zA-Z_]+\.sql"
}

{
    if (match($0, pat)) {
       before = substr($0,1,RSTART-1);

       do {
          pattern = substr($0,RSTART,RLENGTH);
          printf("%s%s\n", before, pattern);
          $0=substr($0, RSTART+RLENGTH)
       } while (match($0, pat))
    }
}

Thank you very much.... my understand is increasing... thanks for the help...:slight_smile: