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