user2844201
user2844201

Reputation: 11

Error in awk code in shell script

I'm using the ls command to list files to be used as input. For each file found, I need to

  1. Perform a system command (importdb) and write to a log file.
  2. Write to an error log file if the first character of column 2, line 6 of the log file created in step 1 is not "0".
  3. rename the file processed so it won't get re-processed on the next run.

My script:

#!/bin/sh
ls APCVENMAST_[0-9][0-9][0-9][0-9]_[0-9][0-9] |
while read LINE 
 do
       importdb -a test901 APCVENMAST ${LINE} > importdb${LINE}.log
 awk "{if (NR==6 && substr($2,1,1) != "0")      
       print "ERROR processing ", ${LINE} > importdb${LINE}err.log
        }" < importdb${LINE}.log
       mv  ${LINE} ${LINE}.PROCESSED
 done 

This is very preliminary code, and I'm new to this, but I can't get passed parsing errors as the one below.

The error context is:

{if (NR==6 && >>>  substr(, <<< awk The statement cannot be correctly parsed.

Upvotes: 1

Views: 1963

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85775

Issues:

  • Never double quote an awk script.
  • Always quote literal strings.
  • Pass in shell variables correctly either by using -v if you need to access the value in the BEGIN block or after the scripts i.e. awk -v awkvar="$shellvar" 'condition{code}' file or by awk condition{code}' awkvar="$shellvar"
  • Always quote shell variables.
  • Conditional should be outside block.
  • There is ambiguity with redirection and concatenation precedence so use parenthesis.

So the corrected (syntactical) script:

 awk 'NR==6 && substr($2,1,1) != 0 {       
           print "ERROR processing ", line > ("importdb" line "err.log")
      }' line="${LINE}" "importdb${LINE}.log"

You have many more issues but as I don't know what you are trying to achieve it's difficult to suggest the correct approach...

  • You shouldn't parse the output of ls
  • Awk reads files you don't need to loop using shell constructs

Upvotes: 6

Related Questions