[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: egrep oddity



On Sun, Feb 05, 2012 at 04:03:45PM -0500, Neal Murphy wrote:
> For quite some time now, I've been getting peeved with egrep not doing what it 
> should.
> 
> I have Squeese installed and up-to-date. In an xterm running bash or on a 
> console running bash or dash, this command:
>   ls -C1 | egrep "^[A-Z]"
> returns all lines except those beginning with 'a'. 

Hi,

When I try, in my home directory:

    ls -C1 | egrep "^[A-Z]"


I get stuff like you probably expect:

    Acrobat Reader
    Class
    Desktop
    Downloads
    Mail
    Maildir

(Note: you probably don't need -C1 if you are piping
the output. ls is smart enough to do that for you
automatically.)

To get the output you complain about -- all lines
except  beginning with 'a' you might use:

    ls | grep "^[^a]"

(In the expression [^somechars], The ^ means to negate
the character class representated by "somechars")

Or more simply, reverse the sense of "^a"

    ls | grep -v "^a"

There is a weirdly named program '[', (see "man ["),
but I doubt it is getting invoked here.

Please recheck you tests and let us know.


> Even the following commands 
> exhibit similar behavior:

>   alias|sed -e 's/^a/b/'|egrep "^[A-Z]"  # passes sed's output untouched
>   alias|sed -e 's/^a/A/'|egrep "^[A-Z]"  # passes sed's output untouched
> 
> These commands behave the same way on another Squeeze installation at another 
> location. Also, 'grep -E' behaves the same way.
> 
> The commands behave as expected on a different GNU/Linux system.
> 
> Does anyone else see this behavior? Or do I need to clean my pipe and smoke 
> something better?

That could explain it. Unix often obeys its own rules.

HTH,

 
> Thanks,
> N
> 
> 
> -- 
-- 
Joel Roth


Reply to: