Unix Shell - Regular expression with egrep question...

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2008 > Regular expression with egrep question...





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Regular expression with egrep question...
somebody

2008-01-03, 7:23 pm

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

Cyrus Kriticos

2008-01-03, 7:23 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 "^[[: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.
Ed Morton

2008-01-03, 7:23 pm



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.

Maxwell Lol

2008-01-04, 1:38 am

somebody <some@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.
somebody

2008-01-04, 1:38 am

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

Stephane Chazelas

2008-01-04, 7:38 am

On Thu, 03 Jan 2008 22:31:38 -0500, somebody wrote:
> On Thu, 03 Jan 2008 18:31:39 -0600, Ed Morton wrote:
>
[...][vbcol=seagreen]
> 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
mik3l3374@gmail.com

2008-01-04, 7:38 am

On Jan 4, 7:31 am, somebody <s...@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
Ed Morton

2008-01-04, 1:27 pm



On 1/4/2008 2:35 AM, mik3l3374@gmail.com wrote:
> On Jan 4, 7:31 am, somebody <s...@body.com> wrote:
>
>
>
> 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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com