csvparse(3am)
π 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 stringfsβ the resulting field separator. defaultCSVFS.commaβ the input CSV field delimiter. defaultCSVCOMMA.quoteβ the input CSV quoting character. defaultCSVQUOTE.
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 stringafieldβ the resulting array of fields.commaβ the input CSV field delimiter. defaultCSVCOMMA.quoteβ the input CSV quoting character. defaultCSVQUOTE.
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 stringquoteβ the input CSV quoting character. defaultCSVQUOTE.
π₯ 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 π¦
- CSV Processing With gawk
csvmode(3am)
π 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
- nixos 26.05: gawkextlib-csv-unstable 2022-10-20
- nixpkgs unstable: gawkextlib-csv-unstable 2022-10-20
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms