PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Regular expression with egrep question...
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Regular expression with egrep question...

Réponse
 
LinkBack Outils de la discussion
Vieux 03/01/2008, 23h31   #1
somebody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 04/01/2008, 00h00   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 04/01/2008, 00h31   #3
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 04/01/2008, 01h33   #4
Maxwell Lol
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression with egrep question...

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.
  Réponse avec citation
Vieux 04/01/2008, 03h31   #5
somebody
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 04/01/2008, 08h04   #6
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 04/01/2008, 08h35   #7
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression with egrep question...

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
  Réponse avec citation
Vieux 04/01/2008, 14h14   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression with egrep question...



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


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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14577 seconds with 16 queries