Regular expression with egrep question...

Regular expression with egrep question...

am 04.01.2008 00:31:11 von some

Why will this regex match lines in myfile.txt which begin with a space:

egrep '^\ ' myfile.txt


And this one does not:

egrep '^\s+' myfile.txt


I thought tat the \s is space, and the + denotes more than one?
I tried using the -e switch to egrep too. Essentially I'm simply
trying to match lines like this:


Line one
Line two
Line three
Line four


-Thanks

Re: Regular expression with egrep question...

am 04.01.2008 01:00:10 von Cyrus Kriticos

somebody wrote:
> Why will this regex match lines in myfile.txt which begin with a space:
>
> egrep '^\ ' myfile.txt
>
>
> And this one does not:
>
> egrep '^\s+' myfile.txt

egrep "^[[:space:]]+" myfile.txt

> I thought tat the \s is space, and the + denotes more than one?
> I tried using the -e switch to egrep too. Essentially I'm simply
> trying to match lines like this:
>
>
> Line one
> Line two
> Line three
> Line four

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Regular expression with egrep question...

am 04.01.2008 01:31:39 von Ed Morton

On 1/3/2008 5:31 PM, somebody wrote:
> Why will this regex match lines in myfile.txt which begin with a space:
>
> egrep '^\ ' myfile.txt
>
>
> And this one does not:
>
> egrep '^\s+' myfile.txt

egrep '[[:blank:]]+' myfile.txt

is probably what you want. "\s" may be a shorthand perl-ism which your egrep may
support when you use the "-P" (for perl) flag.

Ed.

Re: Regular expression with egrep question...

am 04.01.2008 02:33:23 von Maxwell Lol

somebody writes:

> Why will this regex match lines in myfile.txt which begin with a space:
>
> egrep '^\ ' myfile.txt
>
>
> And this one does not:
>
> egrep '^\s+' myfile.txt

perl uses an extension of regexs:

\s == space
\S == nonspace
\d == digit
\D == nondigit
\w == word
\W == nonword

It's nice, but not standard.

Re: Regular expression with egrep question...

am 04.01.2008 04:31:38 von some

On Thu, 03 Jan 2008 18:31:39 -0600, Ed Morton wrote:

> egrep '[[:blank:]]+' myfile.txt
>
> is probably what you want. "\s" may be a shorthand perl-ism which your egrep may
> support when you use the "-P" (for perl) flag.
>
> Ed.


I tried the -P switch specifying perl type regex, but get this error.
Any ideas?

-Thanks


michigan:/home/sombody> egrep -P '^\s+' test.txt
egrep: conflicting matchers specified

Re: Regular expression with egrep question...

am 04.01.2008 09:04:44 von Stephane CHAZELAS

On Thu, 03 Jan 2008 22:31:38 -0500, somebody wrote:
> On Thu, 03 Jan 2008 18:31:39 -0600, Ed Morton wrote:
>
>> egrep '[[:blank:]]+' myfile.txt
>>
>> is probably what you want. "\s" may be a shorthand perl-ism which your egrep may
>> support when you use the "-P" (for perl) flag.
[...]
> I tried the -P switch specifying perl type regex, but get this error.
> Any ideas?
[...]
> michigan:/home/sombody> egrep -P '^\s+' test.txt
> egrep: conflicting matchers specified

egrep is a non-standard shortcut for "grep -E". -E selects
extended regexps, -P perl-compatible regexps, hence the
conflict.

Use grep -P.

Note that \s is [[:space:]], not [[:blank:]] so includes a
number of "space" characters that you may not want matched such
as form feed, vertical tab, carriage return...

[[:blank:]], [[:space:]] called charater class are standard
(POSIX). grep and -E are as well.

egrep is quite common but not POSIX. -P is a GNU extension and
is not available in every GNU grep as it requires the PCRE
library.

Note that the "+" is useless as any line that matches ^\s+ also
matches ^\s and vice versa.

perl -ne 'print if /^\s/'
would be more portable.

--
Stephane

Re: Regular expression with egrep question...

am 04.01.2008 09:35:20 von mik3l3374

On Jan 4, 7:31 am, somebody wrote:
> Why will this regex match lines in myfile.txt which begin with a space:
>
> egrep '^\ ' myfile.txt
>
> And this one does not:
>
> egrep '^\s+' myfile.txt
>
> I thought tat the \s is space, and the + denotes more than one?
> I tried using the -e switch to egrep too. Essentially I'm simply
> trying to match lines like this:
>
> Line one
> Line two
> Line three
> Line four
>
> -Thanks

maybe i am missing something..

sed -n "/^ /p" file

Re: Regular expression with egrep question...

am 04.01.2008 15:14:03 von Ed Morton

On 1/4/2008 2:35 AM, mik3l3374@gmail.com wrote:
> On Jan 4, 7:31 am, somebody wrote:
>
>>Why will this regex match lines in myfile.txt which begin with a space:
>>
>> egrep '^\ ' myfile.txt
>>
>>And this one does not:
>>
>> egrep '^\s+' myfile.txt
>>
>>I thought tat the \s is space, and the + denotes more than one?
>>I tried using the -e switch to egrep too. Essentially I'm simply
>>trying to match lines like this:
>>
>> Line one
>> Line two
>> Line three
>> Line four
>>
>>-Thanks
>
>
> maybe i am missing something..
>
> sed -n "/^ /p" file

That'd find a line that starts with a space char " ", but not one that starts
with a tab " " or other white space char. There's no reason to prefer sed to
*grep for finding patterns in files anyway though.

Ed.