Reputation: 9
I want to replace all occurrences of a character, say ,
, in a matched string in sed
. The matched string looks like:
[cite!abc,cde]
I want to replace it with:
[!cite](abc, cde)
The command to replace the outer format is:
sed 's/\[cite\!\([^]]+\)\]/\[\!cite\]\(\1\)/g' file
which gives
[!cite](abc,cde)
However, I want to put space after ,
and there may be an arbitrary number or comma delimited entries, e.g.
[cite!abc,cde,def,fgh]
Is there an elegant way of doing this in sed
or do I need to resort to perl scripts?
Upvotes: 0
Views: 58
Reputation: 58558
This might work for you (GNU sed):
sed -E ':a;/\[([^]!]+)!([^]]+)\]/{s//\n[!\1](\2)\n/;h;s/,/, /g;H;g;s/\n.*\n(.*)\n.*\n(.*)\n.*/\2\1/;ta}' file
The solution comes in two parts:
The process is then repeated on the current line so as to effect a global replacement.
Upvotes: 0
Reputation: 387
If you're guaranteed no spaces after commas in the input string:
sed 's/,/, /g' file
If you do have spaces after the commas in the input string, you'll get extra spaces in the output.
EDIT:
If there may be spaces after the commas in some of the elements, you can avoid adding more with:
sed 's/,\([^ ]\)/, \1/g' file
$ echo '1, 2, 3,4,5,6' | sed -e 's/,\([^ ]\)/, \1/g'
1, 2, 3, 4, 5, 6
Upvotes: 1