Help with a regular expression???

Amedee Van Gasse (ub) amedee-ubuntu at
Wed Jan 27 14:53:31 UTC 2010

On Wed, January 27, 2010 15:06, Kevin O'Gorman wrote:

> However that may be, I would suggest a perl filter, which is a one-liner
> suitable for a pipeline.
>    perl -p -e 's/    ([^ ])/_ \1/g;'
> translation:
>    -p: copy everything in a loop
>    -e: statement for the loop follows
>    s/   /   /g:   do a substitution on everything (g=multiple times on a
> line)
>    (  ): remember this
>    [^ ]: a character class consisting of any one non-space character.
> Probably does not match newlines either.
>    \1  : the first remembered thing
> Hope this helps.

The same regex syntax can be used with the bash command sed:

sed 's/    ([^ ])/_ \1/g'

This *may* be a bit faster than using heavyweight perl. OTOH perl can do a
lot more than sed, so it depends on what you want to do.


More information about the ubuntu-users mailing list