csvmode(3am)
π csvmode(3am) β like, the CSV one π
okay bestie so real talk: this is csvmode, a page from the GNU Awk Extension Modules set, and it's genuinely one of my faves because it lets gawk justβ¦ eat CSV files directly. no preprocessing era, we're going straight in β¨
β¨ NAME β¨
csvmode β direct processing of Comma-Separated-Values (CSV) data files with gawk. π
π SYNOPSIS π
@include "csv"
BEGIN { CSVMODE = 1 }
... rules with $0, $1, ... $NF, CSVRECORD, ...
csvfield(name, default)
csvprint(record, option...)
csvprint0()
π¦ DESCRIPTION π¦
The gawk-csv extension can directly process CSV data files. it uses a few specific variables, and honestly? they're the whole personality of this module πΈ
CSVMODEβ setCSVMODE=1and CSV formatted input data records automatically get converted into regular awk records with fixed field separators, delivered as$0.$1..$NFare set accordingly too, no cap. SetCSVMODE=0and it disables the conversion, so input files are processed the usual way. see NOTE 1 π
The conversion can be customized by some control variables:
CSVFSβ the resulting field separator, which temporarily overrides theFSandOFSpredefined variables. if it's not set, a null char'\0'is used. see NOTE 1.CSVCOMMAβ the input CSV field delimiter. default comma','.CSVQUOTEβ the input CSV quoting character. default double quote'"'.CSVRECORDβ the original CSV input record. the raw one, unbothered, main character energy π
If the CSV file has a header record, you can also access the fields by name, which is such a slay:
π the functions π
csvfield(name [, missing])β returns the named field of the current record. if there's no column named name, then it returns missing, or a null value if missing isn't given. simple as π«Άcsvprint([record, [fs [, comma [, quote]]]])β a convenience function that formats and prints the given record in a single call. called with no arguments, it prints either$0formatted as CSV orCSVRECORD, depending onCSVMODE. arguments work likecsvformat(). β¨csvprint0()β a convenience function that prints the original input record as such. prints either$0orCSVRECORD, depending onCSVMODE. π
β οΈ heads up bestie:CSVMODE,CSVFS,CSVCOMMAandCSVQUOTEare checked only atBEGINFILEtime. changing them in the middle of a file processing takes no effect. like, none. don't try it π
CSVRECORD is updated for each CSV input record. π¦
The CSV input mode accepts fields with embedded newlines, tabs and other control characters, except null characters ('\0'). π
π EXAMPLES π
Extract CSV records with some specific value in the second field:
BEGIN {CSVMODE = 1}
$2=="some value" {print CSVRECORD}
Process CSV files with fields separated by semicolons instead of commas:
BEGIN {CSVMODE = 1; CSVFS = ";"}
... processing rules ...
Print a specific named field of every record:
BEGIN {CSVMODE = 1;}
{ print csvfield("City") }
Print records that contain commas as data, in both normal and CSV modes:
grepcommas.awk:
BEGINFILE {
CSVMODE = (FILENAME ~ /\.csv$/)
}
/,/ { csvprint0() }
Sample invocation:
gawk -f grepcommas.awk a.txt, b.csv, c.txt
π§ NOTES π§
(1) If the user code has a BEGINFILE action that sets CSV-mode variables depending on the current file, this action must appear before the @include "csv" clause:
BEGINFILE {
CSVMODE = (FILENAME ~ /\.csv$/) # switch mode depending on the file type
}
@include "csv"
like, order matters here fr fr π
π BUGS π
Null characters are not allowed in fields. a null character terminates the record processing. that's it, that's the bug, and yes it will ruin your whole afternoon if you let it π
π AUTHOR π
Manuel Collado, m-collado@users.sourceforge.net.
β SEE ALSO β
XML Processing With gawk, csvparse(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 π«Ά this one shipped as part of the GAWK Extension Library (gawkextlib), dated May 2016 β vintage but still iconic πβ¨π
π¦ 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