UNIX shell script and escaping quotes for AWK script

前端 未结 3 1023
情书的邮戳
情书的邮戳 2021-01-28 04:23

I have a UNIX script that has nawk block inside it (This is just a part of the UNIX and NAWK script. It has many more logic and the below code should definitely be in nawk) This

3条回答
  •  走了就别回头了
    2021-01-28 05:23

    You need to understand where the Unix shell is handling quotes and where Awk is handling quotes.

    Given your need for both single quotes and double quotes in the script, I think you would be best off using an awk program file to contain the script, and then using:

    awk -f awk.script [file1 ...]
    

    This avoids all the issues of whether the shell is going to understand it or not.

    If you can't do that, then you should probably continue using single quotes to surround the awk script, but each occurrence of

    '
    

    inside the script must be replaced by:

    '\''
    

    The first quote terminates the prevailing single-quoted string. The backslash-quote embeds a single quote into the string. The third quote resumes normal single-quoted string operation, where the only special character is single quote.

提交回复
热议问题