[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] There are cases where regexp in FPAT does not work
From: |
Wolfgang Laun |
Subject: |
Re: [bug-gawk] There are cases where regexp in FPAT does not work |
Date: |
Sat, 9 Feb 2019 06:31:31 +0100 |
Printing more shows what's going on:
$ echo '{{111}} design will serve {{111}} ...' | \
awk '{ print FPAT; print $1; print $2; gsub($1,"xxx",$0); print }'
FPAT='{[0-9]+}}'
{[0-9]+}}
{111}}
{111}}
{xxx design will serve {xxx ...
FPAT defines a pattern to match the contents of fields $1, $2,... and this
succeeds, as the print statements document.
The gsub uses $1 as a pattern to replace all of its occurrences in $0.
Since "{111}}" occurs twice, it is replaced twice.
echo '[[111]] design will serve [[111]] ...' | awk '{ print FPAT; print
$1; print $2; gsub($1,"xxx",$0); print }' FPAT='\\[\\[[0-9]+]]'
\[\[[0-9]+]]
[[111]]
[[111]]
[[11xxx] design will serve [[11xxx] ...
Here the field $1 is a strange pattern: a single '[' or '1' (due to /[[1]/,
followed by a ']'. This occurs as "1]", twice, and is correctly replaced.
It is probably not a good idea to use data that may contain any magic
characters as a pattern as in "gsub($1...)".
Wolfgang
On Fri, 8 Feb 2019 at 22:46, Hyunho Cho <address@hidden> wrote:
> $ awk --version
> GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.1, GNU MP 6.1.2)
> Copyright (C) 1989, 1991-2018 Free Software Foundation.
>
> $ lsb_release -a
> No LSB modules are available.
> Distributor ID: Ubuntu
> Description: Ubuntu 18.10
> Release: 18.10
> Codename: cosmic
>
> $ uname -a
> Linux EliteBook 4.18.0-14-generic #15-Ubuntu SMP Mon Jan 14 09:01:02
> UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
>
>
> #####################################################################
>
>
> There are cases where regexp in FPAT does not work.
> If i change "{{111}}" to "{{AAA}} in input string and
> FPAT='{{[A-Z]+}}' then it WORK
>
>
> ### FPAT='{{[0-9]+}}' NOT WORK!
> $ echo '{{111}} design will serve {{111}} ...' |
> awk '{ gsub($1,"xxx",$0); print }' FPAT='{{[0-9]+}}'
> {{111}} design will serve {{111}} ...
>
> ### FPAT='{{[0-9]+}' NOT WORK!
> $ echo '{{111}} design will serve {{111}} ...' |
> awk '{ gsub($1,"xxx",$0); print }' FPAT='{{[0-9]+}'
> {{111}} design will serve {{111}} ...
>
> ### FPAT='{[0-9]+}' WORK!
> $ echo '{{111}} design will serve {{111}} ...' |
> awk '{ gsub($1,"xxx",$0); print }' FPAT='{[0-9]+}'
> {xxx} design will serve {xxx} ...
>
> ### FPAT='{[0-9]+}}' WORK!
> $ echo '{{111}} design will serve {{111}} ...' |
> awk '{ gsub($1,"xxx",$0); print }' FPAT='{[0-9]+}}'
> {xxx design will serve {xxx ...
>
> ### WORK!
> $ echo '{{111}} design will serve {{111}} ...' |
> awk '{ gsub("{{[0-9]+}}","xxx",$0); print }'
> xxx design will serve xxx ...
>
> ### NOT WORK!
> $ echo '[[111]] design will serve [[111]] ...' |
> awk '{ gsub($1,"xxx",$0); print }' FPAT='\\[\\[[0-9]+]]'
> [[11xxx] design will serve [[11xxx] ...
>
> ### WORK!
> $ echo '[[111]] design will serve [[111]] ...' |
> awk '{ gsub("\\[\\[[0-9]+]]","xxx",$0); print }'
> xxx design will serve xxx ...
>
>