Regular expression with egrep question...

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
some [ Fr, 04 Januar 2008 00:31 ] [ ID #1899376 ]

Re: Regular expression with egrep question...

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.
Cyrus Kriticos [ Fr, 04 Januar 2008 01:00 ] [ ID #1899377 ]

Re: Regular expression with egrep question...

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.
Ed Morton [ Fr, 04 Januar 2008 01:31 ] [ ID #1899380 ]

Re: Regular expression with egrep question...

somebody <some [at] body.com> 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.
Maxwell Lol [ Fr, 04 Januar 2008 02:33 ] [ ID #1899381 ]

Re: Regular expression with egrep question...

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
some [ Fr, 04 Januar 2008 04:31 ] [ ID #1899383 ]

Re: Regular expression with egrep question...

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
Stephane CHAZELAS [ Fr, 04 Januar 2008 09:04 ] [ ID #1899387 ]

Re: Regular expression with egrep question...

On Jan 4, 7:31 am, somebody <s... [at] body.com> 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
mik3l3374 [ Fr, 04 Januar 2008 09:35 ] [ ID #1899388 ]

Re: Regular expression with egrep question...

On 1/4/2008 2:35 AM, mik3l3374 [at] gmail.com wrote:
> On Jan 4, 7:31 am, somebody <s... [at] body.com> 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.
Ed Morton [ Fr, 04 Januar 2008 15:14 ] [ ID #1899396 ]
Linux » comp.unix.shell » Regular expression with egrep question...

Vorheriges Thema: assign command result to variable
Nächstes Thema: little sed help