Help with a regular expression???

Ray Parrish crp at cmc.net
Wed Jan 27 19:07:02 UTC 2010


James Michael Fultz wrote:
> * 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.
>   
Hello again,

That has gotten it perfectly now. Thank you! I have the following code -

DescriptionSection="  - Multidimensional Array Objects
    * Efficient arrays of homogeneous machine types (floats, longs,
    complex doubles)
    * Arbitrary number of dimensions
      line continuation.
    * Sophisticated structural operations
  - Universal Function Objects
    * Support mathematical functions on all Python objects
    - Very efficient for array objects"
DescriptionSection=`echo "$DescriptionSection" | sed 's/      \([^ 
\*-]\)/_ \1/g'`
DescriptionSection=`echo "$DescriptionSection" | sed 's/    \([^ 
\*-]\)/_ \1/g'`
echo "$DescriptionSection"

Which outputs -

  - Multidimensional Array Objects
    * Efficient arrays of homogeneous machine types (floats, longs,
_ complex doubles)
    * Arbitrary number of dimensions
_ line continuation.
    * Sophisticated structural operations
  - Universal Function Objects
    * Support mathematical functions on all Python objects
    - Very efficient for array objects

Now I can finally run my other code over the DescriptionSection variable 
to format it's text lists into HTML lists.

Later, Ray Parrish


-- 
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list