1

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.
4

1 回答 1

6

问题:

  • 永远不要双引号awk脚本。
  • 始终引用文字字符串。
  • -v如果您需要访问BEGIN块中的值或在脚本之后 awk -v awkvar="$shellvar" 'condition{code}' file通过使用正确传递 shell 变量awk condition{code}' awkvar="$shellvar"
  • 始终引用 shell 变量。
  • 有条件的应该在块外。
  • 重定向和连接优先级存在歧义,因此请使用括号。

所以更正的(语法)脚本:

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

您还有更多问题,但是由于我不知道您要达到什么目的,因此很难提出正确的方法...

  • 你不应该解析的输出ls
  • awk 使用 shell 结构读取不需要循环的文件
于 2013-10-03T21:08:27.330 回答