csvparse(3am)

πŸ“¦ version 2022-10, in nixos 26.05, nixpkgs unstable

πŸ’… csvparse β€” the gawk CSV glow-up

ok so this is csvparse(3am), bestie β€” the manual page for the GNU Awk extension modules that handle CSV, and honestly? it's giving "finally, I don't have to hand-parse commas myself." πŸŽ€


✨ NAME ✨

csvconvert, csvsplit β€” facilities for parsing Comma-Separated-Values (CSV) data with gawk. yes babes, that's the whole vibe: CSV parsing, but make it awk. πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

@include "csv"
CSVFS = ...
CSVCOMMA = ...
CSVQUOTE = ...
result = csvconvert(csvrecord, option...)
n = csvsplit(csvrecord, afield, option...)
result = csvunquote(csvfield, option)      (see NOTE 1)

look at that. three functions, three variables, one @include and you're literally all set. 🌸


🧚 DESCRIPTION 🧚

The csv gawk extension adds functions for parsing CSV data in a simple way. periodt. it's not doing anything mysterious β€” it just takes your messy CSV text and hands you back fields you can actually use. πŸ”₯

The predefined CSVFS, CSVCOMMA and CSVQUOTE variables set default values for the optional arguments. so you can set your defaults once and then just vibe. πŸ’…

  • CSVFS β€” the field delimiter used in the resulting clean text record, initialized to a null character '\0'. yes, that's a literal null char and it matters β€” more on that in BUGS, bestie, don't skip it. πŸ’€
  • CSVCOMMA β€” the default field delimiter of the CSV input text, initialized to comma ','. (the "comma" in CSV, if you will. icon behavior.)
  • CSVQUOTE β€” the default quoting character of the CSV input text, initialized to double quote '"'. the thing that makes embedded commas not ruin your whole life. 🫢

🌟 csvconvert(csvrecord [, fs [, comma [, quote]]])

Returns the CSV formatted string argument converted to a regular awk record with fixed field separators. Returns a null string if csvrecord is not a valid string.

the arguments are as follows, and I'm listing every single one because we don't skip:

  • csvrecord β€” the CSV formatted input string. the raw material. the main character. πŸ‘‘
  • fs β€” the resulting field separator. default CSVFS.
  • comma β€” the input CSV field delimiter. default CSVCOMMA.
  • quote β€” the input CSV quoting character. default CSVQUOTE.

🌈 csvsplit(csvrecord, afield [, comma [, quote]]])

Splits the CSV formatted string argument into an array of individual clean text fields and returns the number of fields. Returns -1 if csvrecord is not a valid string. ⚠️ yes, -1 β€” check your return value, iykyk.

the arguments:

  • csvrecord β€” the CSV formatted input string.
  • afield β€” the resulting array of fields. this is where the magic lands. ✨
  • comma β€” the input CSV field delimiter. default CSVCOMMA.
  • quote β€” the input CSV quoting character. default CSVQUOTE.

πŸ’œ csvunquote(csvfield [, quote])

Returns the clean text value of the CSV string argument. Returns a null string if csvfield is not a valid string. it's the little un-wrapper that could. πŸŽ€

  • csvfield β€” the CSV formatted input string.
  • quote β€” the input CSV quoting character. default CSVQUOTE.
heads up: the synopsis line for csvunquote has a (see NOTE 1) next to it. that note is referenced there, so don't come for me β€” I'm just the messenger. πŸ™ˆ

πŸ”₯ EXAMPLES πŸ”₯

process CSV input records as arrays of fields:

{
    csvsplit($0, fields)
    if (fields[2]=="some value") print
}

so clean. so simple. fields[2] said what it said. πŸ’…

process CSV input records as awk regular records:

BEGIN {FS = "\0"}
{
    CSVRECORD = $0
    $0 = csvconvert($0)
    if ($2=="some value") print CSVRECORD
}

notice the BEGIN {FS = "\0"}?? that's the null-character separator from CSVFS doing its thing. the way that works is genuinely iconic. πŸ‘‘


πŸ’€ BUGS πŸ’€

Null characters are not allowed in fields. A null character terminates the record processing.

so like… if your data has a null char in it, the whole thing stops. no cap, that's a real limitation, not a cute quirk. plan accordingly bestie. 😭


🫢 AUTHOR 🫢

Manuel Collado, m-collado@users.sourceforge.net.


🌸 SEE ALSO 🌸

go read those too, they're the supporting cast and they ate. πŸ¦‹


πŸ‘‘ COPYING PERMISSIONS πŸ‘‘

Copyright (C) 2018, Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of this manual page provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual page under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual page into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.


and that's the whole page, bestie!! πŸŽ€ mwah πŸ’‹ β€” brought to you by the GAWK Extension Library (gawkextlib), Mar 2018. go forth and parse some commas. ⭐

πŸ“¦ where this page comes from