Skip to main content
deleted 90 characters in body
Source Link
arielCo
  • 1.1k
  • 8
  • 12

\x becomes one character in a double-quoted string (just like in most shells and C) before it's regarded as a regex, so you do need to type \\. to construct \..

Now let'sLet's test that (you don't need the parentheses since the alternation operator | has the lowest precedence; also remember that FS needs to be more than one character long to be treated as a REprecedence):

$ echo ./a.b.c | gawk 'BEGIN { FS = "\.|\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
gawk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'
1: 
2: 
3: 
4: 
5: 
6: 
7: 

The warning is telling you that the escape sequence in the string is superfluous. So FS is .|./ and you're splitting on every character, yielding a bunch of empty fields.

Now with the doubled-up \:

$ echo ./a.b.c | gawk 'BEGIN { FS = "\\.|\\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
1: 
2: a
3: b
4: c

\x becomes one character in a double-quoted string (just like in most shells and C) before it's regarded as a regex, so you do need to type \\. to construct \..

Now let's test that (you don't need the parentheses since the alternation operator | has the lowest precedence; also remember that FS needs to be more than one character long to be treated as a RE):

$ echo ./a.b.c | gawk 'BEGIN { FS = "\.|\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
gawk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'
1: 
2: 
3: 
4: 
5: 
6: 
7: 

The warning is telling you that the escape sequence in the string is superfluous. So FS is .|./ and you're splitting on every character, yielding a bunch of empty fields.

Now with the doubled-up \:

$ echo ./a.b.c | gawk 'BEGIN { FS = "\\.|\\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
1: 
2: a
3: b
4: c

\x becomes one character in a double-quoted string (just like in most shells and C) before it's regarded as a regex, so you do need to type \\. to construct \..

Let's test that (you don't need the parentheses since the alternation operator | has the lowest precedence):

$ echo ./a.b.c | gawk 'BEGIN { FS = "\.|\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
gawk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'
1: 
2: 
3: 
4: 
5: 
6: 
7: 

The warning is telling you that the escape sequence in the string is superfluous. So FS is .|./ and you're splitting on every character, yielding a bunch of empty fields.

Now with the doubled-up \:

$ echo ./a.b.c | gawk 'BEGIN { FS = "\\.|\\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
1: 
2: a
3: b
4: c
Source Link
arielCo
  • 1.1k
  • 8
  • 12

\x becomes one character in a double-quoted string (just like in most shells and C) before it's regarded as a regex, so you do need to type \\. to construct \..

Now let's test that (you don't need the parentheses since the alternation operator | has the lowest precedence; also remember that FS needs to be more than one character long to be treated as a RE):

$ echo ./a.b.c | gawk 'BEGIN { FS = "\.|\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
gawk: cmd. line:1: warning: escape sequence `\.' treated as plain `.'
1: 
2: 
3: 
4: 
5: 
6: 
7: 

The warning is telling you that the escape sequence in the string is superfluous. So FS is .|./ and you're splitting on every character, yielding a bunch of empty fields.

Now with the doubled-up \:

$ echo ./a.b.c | gawk 'BEGIN { FS = "\\.|\\./" } { for (i=1; i<=NF; i++) { print i ": " $i } }'
1: 
2: a
3: b
4: c