sed: replace any number of occurrences of a certain pattern

5

Given this input:

 "hell -- 'this -- world --is'-- beautiful' --thanks-- we'-- are-- here" 

I want to replace every '--' in between single quotes with 'X-X-X' using sed. It should give the following output:

output: "hell --'this X-X-X world X-X-Xis'-- beautiful' X-X-Xthanks X-X-X we' -- are here"

The number of replacements may be unknown (up to infinity).

shiva

Posted 2011-03-18T17:40:03.863

Reputation: 51

Answers

8

Edit:

Using your new requirement:

sed 's/\o47[^\o47]*\o47/\n&\n/g;:a;s/\(\n\o47[^\n]*\)--/\1X-X-X/;ta;s/\n//g' input file

Edit 2:

For some versions of sed which don't like semicolons:

sed -e 's/\o47[^\o47]*\o47/\n&\n/g' -e ':a' -e 's/\(\n\o47[^\n]*\)--/\1X-X-X/' -e 'ta' -e 's/\n//g' inputfile

If your sed also doesn't support octal escape codes:

sed -e "s/'[^']*'/\n&\n/g" -e ':a' -e "s/\(\n'[^\n]*\)--/\1X-X-X/" -e 'ta' -e 's/\n//g' inputfile

Original Answer:

You should usually use single quotes to surround the sed script so you don't have to escape characters which may be special to the shell. Even though it's not necessary in this instance it's a good habit to develop.

sed 's/--/X-X-X/g' inputfile

or

var="hell --this -- world is --beaut--iful"
newvar=$(echo "$var" | sed 's/--/X-X-X/g')

Without the g modifier, the replacement is performed on the first match on each line of input. When g is used, each match on each line of input is replaced. You can also do the replacement for particular matches:

$ var="hell --this -- world is --beaut--iful"
$ echo "$var" | sed 's/--/X-X-X/2'
hell --this X-X-X world is --beaut--iful

Paused until further notice.

Posted 2011-03-18T17:40:03.863

Reputation: 86 075

sorry for putting my question wrong way : input: "hell -- 'this -- world --is'-- beautiful' --thanks-- we'-- are-- here" output: "hell --'this X-X-X world X-X-Xis'-- beautiful' X-X-Xthanks X-X-X we' -- are here" – shiva – 2011-03-20T10:56:45.277

@shiva: Please see my edited answer. Also, please edit your question to show this input instead of the one you originally posted. – Paused until further notice. – 2011-03-20T11:12:38.847

@shiva: What is the specific error message you're getting? What version of sed are you using? What is the operating system? Try the new version I've added which separates the parts of the script using -e. – Paused until further notice. – 2011-03-20T14:34:27.437

7

You want to use the /g switch at the end to parse more than one substitution per line.

sed s/--/X-X-X/g

Hyppy

Posted 2011-03-18T17:40:03.863

Reputation: 3 636

5

$ echo "hell --this -- world is --beaut--iful" | sed s"/--/X-X-X/g"
hell X-X-Xthis X-X-X world is X-X-XbeautX-X-Xiful

The key is the g switch: It causes sed to replace all occurrences.

Daniel Beck

Posted 2011-03-18T17:40:03.863

Reputation: 98 421

Thanks for quick replies sorry for putting my question wrong way : Actually my requirements : replace every -- inbetween single quote '' remaining untouched here i am giving input and output input: "hell -- 'this -- world --is'-- beautiful' --thanks-- we'-- are-- here" output: "hell --'this X-X-X world X-X-Xis'-- beautiful' X-X-Xthanks X-X-X we' -- are here"

Thanks – shiva – 2011-03-20T11:03:57.907

Thanks Dainel for quick reply Sorry for putting my question in wrongway I posted my question again with almost same title

Thanks – shiva – 2011-03-20T11:07:21.743

thanks Daniel sed 's/\o47[^\o47]\o47/\n&\n/g;:a;s/(\n\o47[^\n])--/\1X-X-X/;ta;s/\n//g' input file

but "this one giving regular expression Error:????????????????" – shiva – 2011-03-20T11:26:16.827

@shiva: You replied to the wrong answer. I (Dennis) am the one who posted the answer that seems to be giving you an error. I will reply to your comment in a comment attached to my answer. – Paused until further notice. – 2011-03-20T14:28:27.957

0

echo "     -- hell     -- this ------   world       ---- test"  |\
sed -r 's/[ ]+[-]+[ ]+/ x-x-x /g'

Gives:

 x-x-x hell x-x-x this x-x-x world x-x-x test

The character sequence in brackets is matched for a repeat one or more times by the plus symbol.

SYANiDE

Posted 2011-03-18T17:40:03.863

Reputation: 101