Trim empty fields in a given range

Is there some easy way to trim empty fields but only in a given range?
for example say I have csv data that looks like this:

apple,,,Granysmith,,2.50,,TimmysGrocers
Pear,Bartlett,,,,,Park,
peach,,,,Peento,3.00,Garden,TimmysGrocers

is there a way of getting the single field with data between 2-4 and ignoring the rest so that the output looks like

apple,Granysmith,2.50,,TimmysGrocers
Pear,Bartlett,,Park,
peach,Peento,3.00,Garden,TimmysGrocers

hope that makes sense.

In the third line you just remove columns 2-4 with spaces in it, not extracting any data. And you have different "Granysmithes" in your input and output files. Anyway you can try:

sed -r 's/, +,/,/; s/, +,/,/' INPUTFILE

or (for 2-5 fields):

sed -r 's/, +,/,/; s/, +,/,/; s/, +,/,/' INPUTFILE 
1 Like

Hi yazu. thanks I'll give that a try. The capital letter was a mistake, sorry.
Is there a way of doing it for an arbitrary number of fields, I actually need it in the range 2 to 102 but because I do not understand your sed command I cannot adapt it. Could you please elaborate on it.

---------- Post updated at 02:14 PM ---------- Previous update was at 01:17 PM ----------

I tried to wrap my head around it for a while now but I don't understand it at all any further help greatly appreciated.

sed -r 's/, +,/,/'

removes the first empty field and so on. I think it doesn't scale for your real needs. Try to reformulate your problem.

1 Like

Well, I took your advice and tried something different (and messy), an awk script. so, for example to remove blanks between 2 and 105 i would type

./script.awk Inputfile.csv 2 105

It works but at the end of the output I want it also spits out

awk: cannot open 2 (No such file or directory)

why is it trying to open it as a file? I just want it as an argument/parameter to the script, is there a way to avoid this? The script is below

#!/usr/bin/awk -f
BEGIN { FS = ","}
FNR>1 {for (i = 1; i < ARGV[2]; i++) printf $i","; 
 for (i = ARGV[2]; i <= ARGV[3] ; i++) if ($i!="") printf $i",";
 for (i = ARGV[3]+1; i <= NF; i++) printf $i","; print ""}