[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Insertion of extra OFS character into output string
From: |
Neil R. Ormos |
Subject: |
Re: Insertion of extra OFS character into output string |
Date: |
Mon, 13 Mar 2023 18:41:16 -0500 (CDT) |
User-agent: |
Alpine 2.20 (DEB 67 2015-01-07) |
H 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?
I don't have 4.0.2 available to test, but I tested with older and newer
versions.
When I test, I get the result I think I expect from the code you posted.
Also, setting FPAT overrides the effect of having earlier set FS. (I believe
that the most-recently set one among FS, FPAT, and FIELDWIDTHS controls the
field splitting operation.)
echo "1,2" | awk 'BEGIN{FS=","; FPAT="([^,]*)|(\"[^\"]+\")"; OFS="\t"} {$1=$1;
print}' | hexdump -c
0000000 1 \t 2 \n
0000004
It would be easier to help if you would please provide:
the simplest input line that reproduces the problem;
the output you expect; and
the output you are getting.