[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Conflict between FPAT and empty RS
From: |
luciole75w |
Subject: |
Conflict between FPAT and empty RS |
Date: |
Mon, 6 Apr 2020 01:54:24 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 |
Hello,
I'm having an odd behavior with GNU awk when using FPAT and RS together.
Here is the code.
$ awk '{ print ; $2 = "-" ; print }' RS='' FPAT='\\w+' <<<'a b c d'
a b c d
a -
$ ... | xxd -g1 -c8
00000000: 61 20 62 20 63 20 64 0a a b c d.
00000008: 61 20 2d 20 20 20 20 0a a - .
I'd expect the 2nd print to output "a - c d" but as you can see,
modifying the field 2 replaces fields 3 and 4 with spaces in $0. When RS
is set to something other than the empty string (or just not set), the
output is correct.
Accessing a field is a partial workaround only up to that field, not for
fields above. Accessing NF works for all fields. However the workaround
must be used *before* $0 is referenced, otherwise the missing fields
seem really lost.
$ awk '{ $2 = "-" ; $3 ; print }' RS='' FPAT='\\w+' <<<'a b c d'
a - c
$ awk '{ $2 = "-" ; NF ; print }' RS='' FPAT='\\w+' <<<'a b c d'
a - c d
$ awk '{ $2 = "-" ; $0 ; NF ; print }' RS='' FPAT='\\w+' <<<'a b c d'
a -
Another strange thing which may be related is that setting FS='' before
FPAT fixes the behavior.
$ awk '{ $2 = "-" ; print }' RS='' FS='' FPAT='\\w+' <<<'a b c d'
a - c d
But according to the documentation FPAT should override FS, so I'd
actually expect FS to have no effect in this case.
Tested with gawk 4.1.4 and 5.0.1 on Linux Mint 19.3.
Regards,
luciole
- Conflict between FPAT and empty RS,
luciole75w <=