Awk - field separator behavior

馋奶兔 提交于 2020-01-30 08:14:47

问题


I have a file:

CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS

I want to print "$2"

Example 1

It works well:

$ awk -F '\\\\\\\\' '{print $2}' file
WINDOWS

Example 2

It works well:

$ awk -F '\\\\'+ '{print $2}' file
WINDOWS

Example 3

Does not print.

$ awk -F "\\\\\\\\" '{print $2}' file
.

Example 4

So it works well:

$ echo "CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS" | awk -F '\' '{print $2}'
WINDOWS
----------------------------------------------------------------------------------

$ echo "CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS" | awk -F "\\" '{print $2}'
WINDOWS

Questions:

  1. Why 3 example does not work?
  2. How to properly save 3 example?
  3. What is the difference between?:

    a) awk -F '\\\\\\\\'

    b) awk -F "\\\\\\\\"

Thank you for the explanation.


回答1:


Regarding (3) — four backslashes is the difference:

  • Inside single quotes, there are no metacharacters, so the shell passes 8 backslashes to awk.
  • Inside double quotes, the backslash escapes the following character, so the shell passes 4 backslashes to awk.


来源:https://stackoverflow.com/questions/13609479/awk-field-separator-behavior

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!