Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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).

share|improve this question

3 Answers 3

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
share|improve this answer
    
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 Mar 20 '11 at 10:56
    
@shiva: Please see my edited answer. Also, please edit your question to show this input instead of the one you originally posted. –  Dennis Williamson Mar 20 '11 at 11:12
    
@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. –  Dennis Williamson Mar 20 '11 at 14:34
$ 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.

share|improve this answer
    
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 Mar 20 '11 at 11:03
    
Thanks Dainel for quick reply Sorry for putting my question in wrongway I posted my question again with almost same title Thanks –  shiva Mar 20 '11 at 11:07
    
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 Mar 20 '11 at 11:26
    
@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. –  Dennis Williamson Mar 20 '11 at 14:28

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

sed s/--/X-X-X/g
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.