Reputation: 71
Experts,
I have a very basic requirement here to process a string, i.e., I want to replace all but the first of ,
in the string with a space character:
I know to replace all of them, I can do:
$ echo "abc,def,ghi,jkl" | sed 's/,/ /g'
abc def ghi jkl
To replace only the first 1 of them, I can do:
$ echo "abc,def,ghi,jkl" | sed 's/,/ /1'
abc def,ghi,jkl
But how do I replace all but the first one of ","? That is, the desired output would be:
abc,def ghi jkl
Upvotes: 2
Views: 45
Reputation: 58420
This might work for you (GNU sed):
sed -r ':a;s/(,.*),/\1 /;ta' file
This use greed to work backwards through the sting replacing all but the first ,
with spaces.
Alternatively, working forwards use:
sed -r ':a;s/(,[^,]*),/\1 /;ta' file
Or use this, peculiar to GNU:
sed 's/,/ /2g' file
Upvotes: 0
Reputation: 195059
quick and dirty:
sed 's/,/\x99/;s/,/ /g;s/\x99/,/'
or
sed 's/,/\x99 /g;s/\x99 /,/;s/\x99//g'
same idea
Upvotes: 2