Skip to main content

Transcribe a csv column in awk, accounting for empty fields [Resolved]

Let's say I have a csv file with several columns. NF is not constant. At the moment, to replace columns > 4 with lowercase, I'm doing:

#!/usr/bin/awk
#call with `awk -f lowercase.awk in.csv > out.csv`
BEGIN {
    OFS=",";
    FPAT="([^,]+)|(\"[^\"]+\")";
}
{
    for (i=1; i<=NF; i++){
        if(i>4 && $i){
            $i = tolower($i);
        }
    }
    print $0;
}

But this doesn't take into account adjacent field separators (empty columns denoted by repeated commas) in the csv file like ,,,,. How can I take these into account? I don't like the solutions here because they don't generalise well (there's a lot of conditional checking, and my real-world application of this has NF > 40 and < 70).

Unit test:

in.csv

apple,orange,banana,cherry,MELON,"Fruit salad",grape
"Lots of vegtables",CUCUMBER,carrot,potato,RADISH,BeetrOOT
Bread,BAGEL,,,,Croissant

out.csv

apple,orange,banana,cherry,melon,"fruit salad",grape
"Lots of vegtables",CUCUMBER,carrot,potato,radish,beetroot
Bread,BAGEL,,,,croissant

Question Credit: Escher
Question Reference
Asked September 21, 2019
Posted Under: Unix Linux
23 views
2 Answers

Your Answer
D:\Adnan\Candoerz\CandoProject\vQA