Tedee12345
Tedee12345

Reputation: 1210

Awk - field separator behavior

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.

Upvotes: 1

Views: 689

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753765

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.

Upvotes: 3

Related Questions