awk语法错误:源行1的语法错误

时间:2021-02-26 19:14:25

The following is an example of some contents in an input file:

以下是输入文件中某些内容的示例:

                       username: thebestusernamethereis
                    EMAILID: someemail@address.com
               enc_username: NULL
               MACHINE_CODE: NULL
                  FIRSTNAME: John
                   LASTNAME: Doe
                   ADDRESS1: NULL
                   ADDRESS2: NULL
                    COUNTRY: NULL
                       CITY: NULL
                      STATE: NULL
                      ZCODE: NULL
            ACTIVATION_CODE: NULL
             ACCOUNT_STATUS: Y
              CREATION_DATE: 2010-07-02 12:02:33
              DEFAULT_DRIVE: NULL
    EXCHANGE_BACKUP_ALLOWED: N
SYSTEM_STATE_BACKUP_ALLOWED: N
          CHECKSUM_1_METHOD: NULL
          CHECKSUM_2_METHOD: NULL
          additional_flag_1: NULL
          additional_flag_2: NULL
          additional_flag_3: F
                     USEDBY: F
             DATABASE_DRIVE: NULL
              maxquotalimit: 2147483648
              CURQUOTALIMIT: 0
             ECOMMERCE_FLAG: NULL
                      phone:
          cancellation_date: NULL
               proxy_server: sb.domain.com
                 proxy_port: 20000
          proxy_port_server: 10001
                  num_accts: NULL
                 license_cd: NULL
       IDRIVE_DEFAULT_DRIVE: NULL
international_additional_flag_1: M
            move_identifier: NULL
                  subscribe: N
                  client_ip: NULL
               USEDBY_COUNT: 0
           last_update_date: NULL
                  promocode: NULL
           password_support: NULL
          UNDER MAINTENANCE: UNDEFINED

I have an awk line that seems to causing the error in my script. So, I tested the line as follows:

我有一个awk线,它似乎导致了我的脚本错误。因此,我对这条线进行如下测试:

cat inputfile.txt | awk '$1 ~ /^(user_name:|EMAIL:|FIRST_NAME:|LAST_NAME:|CREATION_DATE:|REGISTRATION_STATUS:)$/{printf "%s,",$2\} $1 ~ /REGISTRATION_STATUS:/{print $2\}' inputfile.txt > outputfile.txt

The error I receive is:

我收到的错误是:

awk: syntax error at source line 1
   context is
  $1 ~ /^(user_name:|EMAIL:|FIRST_NAME:|LAST_NAME:|CREATION_DATE:|REGISTRATION_STATUS:)$/{printf >>>  "%s,",$2\ <<< } $1 ~ /REGISTRATION_STATUS:/{print $2\}
awk: illegal statement at source line 1

The strange thing about this error is that the script was fine before I changed an output file name: old = IDL_$output_file.idl'; new = `$output_file.idl'. This is under PRODUCT4 above.

这个错误的奇怪之处在于,在我更改输出文件名之前,脚本没有问题:old = IDL_$output_file.idl';新=“output_file.idl美元”。这是上面的产品4。

So, I replaced this file with a backup that was working without error messages and it is still producing this error.

所以,我用一个没有错误消息的备份替换了这个文件,它仍然产生这个错误。

I do not know what to do to resolve this. I tried changing #!/bin/sh to #!/bin/bash and have the same error. I do not know what the syntax error is.

我不知道该怎么解决这个问题。我试着改变# !/bin/sh # !/bin/bash并有相同的错误。我不知道语法错误是什么。

I am using bash on Mac El Capitan.

我在Mac El Capitan上使用bash。

Please help. Thank you!

请帮助。谢谢你!

2 个解决方案

#1


3  

As the error message is showing you - you have a backslash before every }.

正如错误消息显示的那样,在每个}之前都有一个反斜杠。

#2


1  

You have two statements on one line, with no separating semicolon. Try adding a semicolon or newline before $1 ~ /REGISTRATION_STATUS:/{print $2}

一行上有两个语句,没有分号。尝试在$1 ~ /REGISTRATION_STATUS:/{打印$2}之前添加分号或换行

#1


3  

As the error message is showing you - you have a backslash before every }.

正如错误消息显示的那样,在每个}之前都有一个反斜杠。

#2


1  

You have two statements on one line, with no separating semicolon. Try adding a semicolon or newline before $1 ~ /REGISTRATION_STATUS:/{print $2}

一行上有两个语句,没有分号。尝试在$1 ~ /REGISTRATION_STATUS:/{打印$2}之前添加分号或换行