Re: sed replace everything between...



On Tue, 16 Aug 2011 06:42:26 -0700, Zak wrote:
i'm trying to search and replace a typical expression in a file such as

\{student "7+9"\} with "7+9"

there are multiple instances in the file of \{student * \} and i want to
search and replace everything found with what the regexp wildcard
represents. I have tried this

s/\(\\{student\) \(*\) \(\\}\)/\2/g

to no avail,
Zak

In file globbing, * stands for 'any string', but in sed and grep
regular expressions, ordinarily * allows previous entity to repeat.
But as it happens, \(*\) will match a single asterisk. For example,
echo 'abc***ghi' | sed 's#\(*\)#hey#g'
gives abcheyheyheyghi

In following, the pattern allows any number of spaces before or
after the inner-match text, and no spaces within that match.

echo 'abc\{student "7+9"\}def\{student "soforth" \}ghi' |
sed 's#\\{student *\([^ ]*\) *\\}#\1#g'

gives: abc"7+9"def"soforth"ghi






--
jiw
.