help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Insertion of extra OFS character into output string


From: david kerns
Subject: Re: Insertion of extra OFS character into output string
Date: Mon, 13 Mar 2023 15:56:58 -0700

The problem is your definition of FPAT (I've never used FPAT in all my
years of awk)
$1=$1; # this line causes $0 to be re-built using OFS...
but if you add a print BEFORE the $1=$1 you'll see the issue has already
manifested itself.
when I comment out the FPAT, the issue goes away:

$ cat somefile.csv
1,2,3,4
$ cat p1
 gawk 'BEGIN{
FS=",";
#FPAT="([^,]*)|(\"[^\"]+\")";
OFS="\t"
}
{ print }
{
$1=$1;
print;
gsub(/"/, "");
print
}' somefile.csv
$ bash ./p1
1,2,3,4
1 2 3 4
1 2 3 4
$ gawk --version
GNU Awk 4.0.2
# Cent OS7 :)

... if nothing else, I've provided you some additional debugging


On Mon, Mar 13, 2023 at 1:27 PM H <agents@meddatainc.com> wrote:

> I am a newcomer to awk and have run into an issue I have not figured out
> yet... My platform is CentOS 7 running awk 4.0.2, the default version.
>
> The following awk statement generates an extra tab character between
> fields 1 and 2, regardless of the data in the file:
>
> awk 'BEGIN{FS=","; FPAT="([^,]*)|(\"[^\"]+\")"; OFS="\t"} {$1=$1;
> gsub(/"/, ""); print}' somefile.csv
>
> If i change the statement to:
>
> awk 'BEGIN{FS=","; FPAT="([^,]*)|(\"[^\"]+\")"; OFS="\t"} {$2=$2;
> gsub(/"/, ""); print}' somefile.csv
>
> an extra OFS character is inserted between fields two and three. I can add
> that removing the gsub() in either of the two examples does not affect the
> results.
>
> Might this be a bug in 4.0.2 or a feature I have not yet understood?
>
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]