Portable way to split an external variable containing newlines in awk?

Viewed 82

Consider these awk commands:

#!/bin/bash
awk 'BEGIN { print split("X\nX",a,"\n") }'
awk -v s=$'X\nX' 'BEGIN { print split(s,a,"\n") }'

Results:

  • Linux:
2
2
  • macOS, FreeBSD:
2
/usr/bin/awk: newline in string X
X... at source line 1
  • Solaris:
2
/usr/xpg4/bin/awk: file "(null)": line 1: Newline in string
  Context is:
>>>     X
>>>             <<<

Is there a way to work around that?


Edit:

There's not even the need to use an external variable, the following will also fail in all awk implementations but the GNU one:

awk 'BEGIN { s = "X\nX"; print split(s,a,"\n") }'
1 Answers

POSIX awk does not allow physical newlines in string values.

When you use C/BASH string notation like $'a\nb' then any POSIX compliant awk implementation will fail.

Even with gnu-awk, when you enable posix option following error will be returned:

awk --posix -v s=$'X\nX' 'BEGIN { print split(s,a,"\n") }'
awk: fatal: POSIX does not allow physical newlines in string values

However if you remove $'...' notation then error will not be there:

awk --posix -v s="X\nX" 'BEGIN { print split(s,a,"\n") }'
2
Related