syntax error with awk print

561 Views Asked by At

for a text like the following:

std_out_file: ${ALGO_TOP}/dynamic/batchlog/JS030003-ara.stop.badb.out

I want the output to be:

$BATCHLOG_DIR/JS030003-ara.stop.badb.out

here is the code:

nawk '/std_out_file:|std_err_file:/ { split($2, a, /); if ( a[3]=="batchlog" ) print "$$BATCHLOG_DIR/"$3; else print "$$CONSOLELOG_DIR/"$3; }'

I got the following error:

nawk: illegal primary in regular expression ); if ( a[3]=="batchlog" ) print "$$BATCHLOG_DIR at ; if ( a[3]=="batchlog" ) print "$$BATCHLOG_DIR
 source line number 4
 context is
        /std_out_file:|std_err_file:/ { split($2, a, /); if ( a[3]=="batchlog" ) print "$$BATCHLOG_DIR/", $3; else print >>>  " <<<

where did I get it wrong?

1

There are 1 best solutions below

4
On BEST ANSWER

The third argument of split function is a regex. You should place your / inside split function like:

split($2, a, /\//)

Notice that / needs to be escaped to let awk know that is it not start or end of the regex pattern.

Having said that, you can avoid the split altogether by setting the delimiter to /. Something like:

awk -F'/' '/std_out_file:|std_err_file:/{print ($(NF-1)=="batchlog" ? "$BATCH" : "$CONSOLE") "LOG_DIR/"$NF}' file

Where you test the second last field $(NF-1) to be batchlog. The ternary op is basically doing the same thing as your if-else condition.