awk: warning: escape sequence `\]' treated as plain `]'

后端 未结 2 1550
灰色年华
灰色年华 2021-01-05 00:46
echo \"A:\\ B:\\ C:\\ D:\\\" | awk -F\'[:\\]\' \'{print $1}\'

Output:

awk: warning: escape sequence `\\]\' treated as plain `]\'

A         


        
相关标签:
2条回答
  • 2021-01-05 01:23

    I think this is what you're trying to do:

    $ echo 'A:\ B:\ C:\ D:\' | awk -F':\\\\[[:space:]]*' '{print $2}'
    B
    

    Note that I added [[:space:]]* as I don't think you want leading spaces in the fields after $1.

    String literals as used in your FS setting get parsed twice, once when the script is read and again when executed so escapes need to be doubled up (google it and/or read the awk man page or book). In this case on the first pass \\\\ gets converted to \\ and on the second pass \\ gets converted to \ which is what you want.

    0 讨论(0)
  • 2021-01-05 01:25

    This is also wrong:

    echo "A:\ B:\ C:\ D:\"
    

    You are escaping the " and it breaks the input, you need to remove it or use single quote.

    awk does not like a single \ within field separator, so try:

    echo 'A:\ B:\ C:\ D:\' | awk -F":|\\" '{print $1}'
    A
    

    Or you can escape it (single quote):

    echo 'A:\ B:\ C:\ D:\' | awk -F'[:\\\\]' '{print $1}'
    A
    

    Or even more escaping (double quote):

    echo 'A:\ B:\ C:\ D:\' | awk -F"[:\\\\\\\]" '{print $1}'
    A
    
    0 讨论(0)
提交回复
热议问题