On 27/04/2023 17:24, Manuel Collado wrote:
Re-posted - URL changed.
"csvio.awk" is a pure awk library that provides CSV support for awk. It
is available in two variants. "gawk-csvio" uses some specific gawk
features. "awk-csvio" uses only POSIX awk features.
They are available at http://mcollado.z15.es/gawk-extras/.
Version 0.x.x is intended as a preliminary issue, mostly to get feedback
from interested users. Suggestions, comments, bug reports, etc. are
welcome.
The goal of csvio is to process CSV records as if they were regular awk records, delimited by some FS/OFS or your choice.
HTH. Enjoy.
[re-send as first attempt has vanished]
Thanks Manuel, very handy.
Some thoughts based purely on a reading of your code (rather than actual testing), so this may be wrong, but I'm sure the regulars here will
correct me if so. :-)
1) The while loop in csvimport()
You append to $0 each time through the loop. This will cause $1, $2 etc
to be regenerated each time so it would be more efficient to assign to a temporary variable and then assign that to $0 once only after the loop completes. That way $1, $2 etc are only rebuilt once.
2) The "getline more" statement in csvimport()
A malformed file might end in the middle of a multi-line field. It looks
as though your code doesn't detect this whereas it might be better to
abort with an error.
3) The for loop in csvimport()
The statements
for (k=1; k in af; k++) {
fk = af[k]
can be replaced with the single statement
for (fk in af) {
4) The for loop in csvimport()
The line
$0 = $0 ofs fk # Concatenate fields, delimited by OFS
only works because you set FS=OFS='|' in your examples. If they are not
the same then after $0 has been built the values of $1, $2 etc won't
match the fields extracted.
A simple solution would be to build $0 by setting $1, $2 etc using the statement
$k = fk
so that $1, $2 are set as expected and you don't have to worry about
whatever value the user has used for FS.
Making this change negates my point 3, of course. It also allows for the slightly esoteric scenario where the user might want to change the value
of OFS mid-output.
Hope this helps and comments, corrections and improvements from others
welcome.
--
Bruce Horrocks
Surrey, England
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)