help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any way to set "--csv" in BEGIN?


From: Manuel Collado
Subject: Re: Any way to set "--csv" in BEGIN?
Date: Sat, 11 Nov 2023 20:31:36 +0100
User-agent: Mozilla Thunderbird

El 10/11/23 a las 17:11, Ed Morton escribió:
Thanks for the quick response Andy.

It's not a huge deal for me personally as I don't expect to ever NEED that functionality, e.g. I'd normally write a shell script to call any non-trivial awk script so I can always add `--csv` when appropriate from shell, and if I wanted to switch to CSV mode mid-processing to do anything like parse a field that contains CSV then FPAT/patsplit(), or FS/split() if the opposite, are almost certainly good enough. And I could always pipe+getline to a new script in a subshell if absolutely necessary.

Please have a look at the pure [g]awk-csvio library available at
    http://mcollado.z15.es/gawk-extras/

Example, convert a CSV file to tab-delimited file:

gawk '
    BEGIN{ FS=OFS="\t" }
    @include "csvio"
    {
        csvimport()
        print csvrecord()
    }
    ' some_data_file.csv > some_data_file.tab

If you have a script that process regular awk records delimited by a suitable FS, this script can be used also to process CSV records by just adding a single additional first rule: '{ csvimport() }'. That's all.

Well, this approach requires OFS to match FS. As usual.

HTH
--
Manuel Collado - http://mcollado.z15.es



reply via email to

[Prev in Thread] Current Thread [Next in Thread]