I have a file containing this :
$ cat file
PARM1=(VAL11),PARM2=(VAL21,VAL22,VAL23),PARM3=(VAL31),PARM4=(VAL41,VAL42)
and I need to replace all the ',' by '|' but only those which are between brackets.
Output would be :
PARM1=(VAL11),PARM2=(VAL21|VAL22|VAL23),PARM3=(VAL31),PARM4=(VAL41|VAL42)
I almost find a solution with sed :
sed 's/\(([^)]*\),/\1|/g' file
But not all the comma are replaced :
PARM1=(VAL11),PARM2=(VAL21,VAL22|VAL23),PARM3=(VAL31),PARM4=(VAL41|VAL42)
I see a pattern here... All the commas you don't want replaced are right after a bracket. So we match a not-bracket with a comma after it, putting the not-bracket into the backreference, and putting a | where the comma was. So:
$ echo "PARM1=(VAL11),PARM2=(VAL21,VAL22,VAL23),PARM3=(VAL31),PARM4=(VAL41,VAL42)" | sed 's/\([^)]\),/\1|/g'
PARM1=(VAL11),PARM2=(VAL21|VAL22|VAL23),PARM3=(VAL31),PARM4=(VAL41|VAL42)
$
@Corona688 : actually my first example was not correct. In fact, the data that I have to manage is more dirty. Some value is surrounded with brackets, quotes or both, or even nothing. Also, some comma might be surrounded by spaces...
Well I understand that we separate each record with '(' and each field with ')'.
So in the first field of each record we are sure that all comma must be replaced by pipe.
But I don't understand the '1' in {gsub(",","|",$1)}1