|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|