moore1emu
moore1emu

Reputation: 496

loop add a comma after nth comma using awk

I feel like this should be a fairly straight forward question, but I cant seem to get it to work.

I have a csv file and I need to add comma after the nth comma in each row. I believe I have to use gsub to get it to loop. something like

{gsub(/$nth/,/",")}

but I don't understand awk well enough to get it to work.

The end goal of my script is to check to see if a word exist and if it does NOT add a comma after the nth comma.

I'm using grep for that part like this:

TEST1=$(cat $file | grep 'Sentence Skills')
 if [ $? -eq 1 ]
  then
  awk command to add comma after nth comma
 fi

If it doesnt exist I need to add a comma after the nth comma to make sure everything lines up correctly

UPDATE to clarify with sample input and output (my apologies, I originally was not going to include the grep if then fi part)

Here is a sample of the csv:

last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 97,Sentence Skills 104,College Level Math 76,Elementary Algebra 115,
last,first,A00XXXXXX,1888-01-01,2015-05-13,Elementary Algebra 34,
last,first,A00XXXXXX,1888-01-01,2015-05-13,College Level Math 64,Elementary Algebra 114,
last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 87,College Level Math 64,Elementary Algebra 114,

And this is what I need it to look like:

last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 97,Sentence Skills 104,College Level Math 76,Elementary Algebra 115,
last,first,A00XXXXXX,1888-01-01,2015-05-13,,,,Elementary Algebra 34,
last,first,A00XXXXXX,1888-01-01,2015-05-13,,,College Level Math 64,Elementary Algebra 114,
last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 87,,College Level Math 64,Elementary Algebra 114,

I need to add one comma after the 5th comma if Reading comprehension doesn't exist, then add one comma after the 6th comma if Sentence Skills doesn't exist, then one comma after the 7th comma if College Level Math doesn't exist, then one comma if Elementary algebra doesn't exist.

If any of those do exist, it does not add a comma and skips on to the next one.

Upvotes: 0

Views: 216

Answers (1)

Birei
Birei

Reputation: 36262

In my opinion it can be a difficult task to solve it with . Here you have an approach with . The difference is that I can use a hash, sort it and add items in the middle of an array with splice.

perl -F',' -lanE '
    BEGIN {
        %h = ( 
            5 => q|Reading Comprehension|,
            6 => q|Sentence Skills|,
            7 => q|College Level Math|,
            8 => q|Elementary Algebra|,
        );  
    };  
    for (sort keys %h) {
        if ($F[$_] !~ m/^$h{$_}/) {
            splice @F, $_, 0, q||;
            ++$_;
        }   
    }
    printf qq|%s,\n|, join q|,|, @F;
' infile

The -F has same meaning that , so I loop over the hash and check if that field number contains the value. If it doesn't match, insert an empty element just before it, and the last printf joins all fields with comma. It yields:

last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 97,Sentence Skills 104,College Level Math 76,Elementary Algebra 115,
last,first,A00XXXXXX,1888-01-01,2015-05-13,,,,Elementary Algebra 34,
last,first,A00XXXXXX,1888-01-01,2015-05-13,,,College Level Math 64,Elementary Algebra 114,
last,first,A00XXXXXX,1888-01-01,2015-05-13,Reading Comprehension 87,,College Level Math 64,Elementary Algebra 114,

Upvotes: 1

Related Questions