csvio - facilities for reading and composing Comma-Separated-Values (CSV) data with awk.
csvimport([comma
[, quote]])
result = csvrecord(afield [, comma
[, quote]])
result = csvrecord0([comma [,
quote]])
The
csvio.awk library provides functions for reading
and composing CSV data records:
csvimport([comma [, quote]])
Converts the input CSV record into a regular awk record delimited by OFS. Automatically collects additional iput lines to complete the current input record if it contains EOLs embedded in fields.
comma |
Optional. The CSV input field delimiter. Default comma (’,’). | ||
quote |
Optional. The CSV input quoting character. Default double quote (’"’). |
csvrecord(afield [, comma [, quote]])
Returns a CSV formatted string by composing the values in the afield array.
afield |
An array of field values, indexed from 1 to N. | ||
comma |
Optional. The resulting CSV field delimiter. Default comma (’,’). | ||
quote |
Optional. The resulting CSV quoting character. Default double quote (’"’). |
csvrecord0([comma [, quote]])
Returns a CSV formatted string by composing the values of the current fields $1 to $NF.
comma |
Optional. The resulting CSV field delimiter. Default comma (’,’). | ||
quote |
Optional. The resulting CSV quoting character. Default double quote (’"’). |
Import CSV data:
echo '007,"Bond, James",United Kingdom' | gawk ' BEGIN {FS=OFS="|"} { csvimport() # --> $0 = "007|Bond, James|United Kingdom" }'
Compose from an array of field values:
f[1] = "007" f[2] = "Bond, James" f[3] = "United Kingdom" result = csvrecord(f) # --> '007,"Bond, James",United Kingdom'
Compose from the current field values:
FS=OFS="|" $0 = "007|Bond, James|United Kingdom" result = csvrecord0() # --> '007,"Bond, James",United Kingdom'
CSV fields with embedded EOLs are not supported when reading with redirected getline.
Manuel Collado, m-collado@users.sourceforge.net.
Public domain. See UNLICENSE for details.