Hi experts,
My file looks something like this.
abcXX4,7,234
abc,defg,45XX23,74,123
The number of commas left of the XX can vary. The question is how can I replace all the commas left of the 'XX' with an underscore?
abcXX4,7,234
abc_defg_45XX23,74,123
Thanks!
Try:
awk '{gsub(/,/,"_",$1)}1' FS=XX OFS=XX file
2 Likes
in bash:
while read a ; do echo ${a//,/_} ; done < file
You missed the "until pattern" qualification.
Regards,
Alister
sed -e ':a' -e '/,.*XX/ s/,/_/; ta' file
This is little different (replaces up to the rightmost XX), e.g. in this case
abc,defg,45XX23,1XX1,74,123
Hi,
Just for fun, another sed solution but catch first XX occurence:
sed '/[^,]*XX/{;s//\n&/;h;s/\n.*$//;y/,/_/;G;s/\n.*\n//;}' file
Example:
$ echo 'abc,defg,45XX23,1XX1,74,123' | sed '/[^,]*XX/{;s//\n&/;h;s/\n.*$//;y/,/_/;G;s/\n.*\n//;}'
abc_defg_45XX23,1XX1,74,123
Regards.