Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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") }'

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading