csvparse(3am)

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

πŸ’… csvparse(3am) β€” the CSV parsing page, but make it cute

okay so this is csvparse(3am), straight out of the GNU Awk Extension Modules family, and bestie it's about to make CSV parsing actually bearable ✨


🌸 NAME 🌸

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


πŸŽ€ SYNOPSIS πŸŽ€

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

✨ DESCRIPTION ✨

the csv gawk extension adds functions for parsing CSV data in a simple way. literally that's it, that's the whole personality. πŸ¦‹

and listen β€” the predefined CSVFS, CSVCOMMA and CSVQUOTE variables set default values for the optional arguments. so you can set your defaults once and then live your life. πŸ‘‘

  • CSVFS β€” the field delimiter used in the resulting clean text record, initialized to a null character '\0'. yes, a null character. iconic but also relevant later, keep that in mind πŸ’€
  • CSVCOMMA β€” the default field delimiter of the CSV input text, initialized to comma ','.
  • CSVQUOTE β€” the default quoting character of the CSV input text, initialized to double quote '"'.

🌈 the functions 🌈

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

returns the CSV formatted string argument converted to a regular awk record with fixed field separators. slay. and if csvrecord is not a valid string it returns a null string β€” so like, it's not gonna yell at you, it just goes quiet 😭

the arguments, one by one:

  • csvrecord β€” the CSV formatted input string
  • 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. that's the main character move right there 🫢

and heads up: returns -1 if csvrecord is not a valid string. not a null string, not zero β€” -1. so check for that, iykyk.

the arguments:

  • csvrecord β€” the CSV formatted input string
  • afield β€” the resulting array of fields.
  • 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. and if csvfield is not a valid string, you get a null string back. 🧚

the arguments:

  • csvfield β€” the CSV formatted input string
  • quote β€” the input CSV quoting character. default CSVQUOTE.

πŸ”₯ EXAMPLES πŸ”₯

process CSV input records as arrays of fields:

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

process CSV input records as awk regular records:

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

like, look at that second one β€” FS = "\0", remember CSVFS defaults to a null character? it all comes together. we love a consistent queen πŸ’…


πŸ’€ BUGS πŸ’€

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

fr fr, do not put null characters in your fields. a null character terminates the record processing, and that's just how it is. no cap. 😭


πŸ‘‘ AUTHOR πŸ‘‘

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


πŸ¦‹ SEE ALSO πŸ¦‹


πŸ“œ 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 πŸŽ€ brought to you by the GAWK Extension Library (gawkextlib), dated Mar 2018 β€” csvparse(3am), out. πŸ’–βœ¨

πŸ“¦ where this page comes from