user2585500
user2585500

Reputation: 9

How can I replace all occurrences of a character within matched string in sed

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

Answers (2)

potong
potong

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:

  1. The string to be amended are identified, and a first pass rearranges to the requires format less the comma space separated list.
  2. The string is surrounded by newlines and a copy is made into the hold space. Then the current pattern space is has all commas space separated and the copy and current line are joined and using patten matching formatted to final required state.

The process is then repeated on the current line so as to effect a global replacement.

Upvotes: 0

LNO
LNO

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

Related Questions