1

I have a csv file with below data

Input

"01","5","Male","[""No.**"",""**kg""]","","",""  
"02","6","FeMale","[""No.""]","","",""

I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.

Output:

"01","5","Male","[""No.**,**kg""]","","",""  
"02","6","FeMale","[""No.""]","","",""

I tried below command, but it does replace data outside the square brackets, but not for all lines.

sed '/\[/,/\]/s/"",""/,/'
3
sed '/\[/,/\]/s/"",""/,/'

This would look for a line with a [, then a line with a ], and between those lines, replace the first "","" encountered on each line. It doesn't really look at where the [..] are within a line.


As a zero-order try, something like this:

$ cat x
"01","5","Male","[""No.**"",""**kg""]","","","" 
$ sed 's/\(\[.*\)"",""\(.*\]\)/\1,\2/g' x
"01","5","Male","[""No.**,**kg""]","","","" 

The pattern matches a [, anything, "","", anything and a ], while capturing all but the "","" so it can put the pieces back together.

This will break for stuff like [..],"","",[..] (where the brackets close before a "","" is seen, the pattern searches for a following ]) and [.."","".."",""..] (with multiple "","" sequences inside the brackets, only one is removed).

Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:

$ cat y
no removal here: [...],"","",[...]
double removal here: [  "",""  "",""  ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]

$ perl -pe 'sub x {$a = shift; $a =~ s/"",""/,/g; return $a;}
     s/(\[.*?\])/ x($1)  /eg  ' y
no removal here: [...],"","",[...]
double removal here: [  ,  ,  ]
[""remove,here""],""not"",""here"",[""also,here""]

(.*? is a non-greedy match, it stops as soon as it can, that is, at the first ] in this case.)

0
sed '/"Male/s/"",""/,/1' filename

Output:

"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.