Help with a regular expression???

James Michael Fultz croooow at gmail.com
Wed Jan 27 18:09:04 UTC 2010


* James Michael Fultz <croooow at gmail.com> [2010-01-27 13:04 -0500]:
> * Ray Parrish <crp at cmc.net> [2010-01-27 09:04 -0800]:
> > Amedee Van Gasse (ub) wrote:
> [...]
> > > The same regex syntax can be used with the bash command sed:
> > >
> > > sed 's/    ([^ ])/_ \1/g'
> [...]
> > DescriptionSection=`echo | sed 's/    ([^ ])/_ \1/g'`
> > echo "$DescriptionSection"
> > 
> > And I get the following cryptic error, which tells me nothing. Can you help?
> > 
> > sed: -e expression #1, char 19: invalid reference \1 on `s' command's RHS
> 
> DescriptionSection=`echo | sed 's/    \([^ ]\)/_ \1/g'`
[...]

Oops!  Overlooked one other thing.

DescriptionSection=`echo "$SomeVar" | sed 's/    \([^ ]\)/_ \1/g'`

Where '$SomeVar' is a variable whose contents you wish to process.
Also, the following form using printf is actually safer.

DescriptionSection=`printf '%s\n' "$SomeVar" | sed 's/    \([^ ]\)/_ \1/g'`

The reason printf is safer than echo being that printf ignores
subsequent options (strings starting with a dash), whereas echo may
misinterpret an arbitrary string beginning with a dash as an option.




More information about the ubuntu-users mailing list