From: Tom Harrington on 21 Jan 2010 17:16 In article <-bmdndcRgr-txsXWnZ2dnUVZ7tSdnZ2d(a)pipex.net>, TF <awax46(a)dsl.pipex.com> wrote: > On Thu Jan 21 2010 at 10:30:39 UTC, JF Mezei <jfmezei.spamnot(a)vaxination.ca> > wrote: > > Sorry for the newbie question: > > > > what is the grep syntax to look for the string "velo" but NOT > > "development" ? > > You can do that "properly" with regular expressions but, at least in > simple cases, I find myself piping multiple instances of grep instead, > e.g. > > cat file.txt | grep "velo" | grep -v "development" > > Here, "-v" makes grep perform what's called an "inverted matching", that > is, it passes everything through that does NOT match "development". If you mean "velo" as a word on its own, you can use: grep '\<velo\>' file.txt That'll find "velo" with a word boundary on either side. If that's not what you want then I'd just use TF's approach. -- Tom "Tom" Harrington Independent Mac OS X developer since 2002 http://www.atomicbird.com/
From: David Empson on 21 Jan 2010 18:18 Tom Harrington <tph(a)pcisys.no.spam.dammit.net> wrote: > In article <-bmdndcRgr-txsXWnZ2dnUVZ7tSdnZ2d(a)pipex.net>, > TF <awax46(a)dsl.pipex.com> wrote: > > > On Thu Jan 21 2010 at 10:30:39 UTC, JF Mezei <jfmezei.spamnot(a)vaxination.ca> > > wrote: > > > Sorry for the newbie question: > > > > > > what is the grep syntax to look for the string "velo" but NOT > > > "development" ? > > > > You can do that "properly" with regular expressions but, at least in > > simple cases, I find myself piping multiple instances of grep instead, > > e.g. > > > > cat file.txt | grep "velo" | grep -v "development" > > > > Here, "-v" makes grep perform what's called an "inverted matching", that > > is, it passes everything through that does NOT match "development". > > If you mean "velo" as a word on its own, you can use: > > grep '\<velo\>' file.txt > > That'll find "velo" with a word boundary on either side. If that's not > what you want then I'd just use TF's approach. That's one I didn't know about, so thanks for the pointer. On further research it seems to be specific to grep and not to other regular expressions (e.g. Perl doesn't know it). A similar regular expression which is compatible with GNU grep and Perl is: grep '\bvelo\b' file.txt The '\b' matches a zero-length word boundary without caring if it is at the beginning or end of a word. -- David Empson dempson(a)actrix.gen.nz
From: Barry Margolin on 21 Jan 2010 18:56 In article <1jcpydn.1lefot27nxzewN%dempson(a)actrix.gen.nz>, dempson(a)actrix.gen.nz (David Empson) wrote: > Tom Harrington <tph(a)pcisys.no.spam.dammit.net> wrote: > > > In article <-bmdndcRgr-txsXWnZ2dnUVZ7tSdnZ2d(a)pipex.net>, > > TF <awax46(a)dsl.pipex.com> wrote: > > > > > On Thu Jan 21 2010 at 10:30:39 UTC, JF Mezei > > > <jfmezei.spamnot(a)vaxination.ca> > > > wrote: > > > > Sorry for the newbie question: > > > > > > > > what is the grep syntax to look for the string "velo" but NOT > > > > "development" ? > > > > > > You can do that "properly" with regular expressions but, at least in > > > simple cases, I find myself piping multiple instances of grep instead, > > > e.g. > > > > > > cat file.txt | grep "velo" | grep -v "development" > > > > > > Here, "-v" makes grep perform what's called an "inverted matching", that > > > is, it passes everything through that does NOT match "development". > > > > If you mean "velo" as a word on its own, you can use: > > > > grep '\<velo\>' file.txt > > > > That'll find "velo" with a word boundary on either side. If that's not > > what you want then I'd just use TF's approach. > > That's one I didn't know about, so thanks for the pointer. On further > research it seems to be specific to grep and not to other regular > expressions (e.g. Perl doesn't know it). Yes it does, but it uses < and > rather than \< and \>. Perl only uses backslash before letters in its regular expressions, punctuation characters that have special meanings are never backslashed. -- Barry Margolin, barmar(a)alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***
From: David Empson on 21 Jan 2010 19:24 Barry Margolin <barmar(a)alum.mit.edu> wrote: > In article <1jcpydn.1lefot27nxzewN%dempson(a)actrix.gen.nz>, > dempson(a)actrix.gen.nz (David Empson) wrote: > > > Tom Harrington <tph(a)pcisys.no.spam.dammit.net> wrote: > > > > > In article <-bmdndcRgr-txsXWnZ2dnUVZ7tSdnZ2d(a)pipex.net>, > > > TF <awax46(a)dsl.pipex.com> wrote: > > > > > > > On Thu Jan 21 2010 at 10:30:39 UTC, JF Mezei > > > > <jfmezei.spamnot(a)vaxination.ca> > > > > wrote: > > > > > Sorry for the newbie question: > > > > > > > > > > what is the grep syntax to look for the string "velo" but NOT > > > > > "development" ? > > > > > > > > You can do that "properly" with regular expressions but, at least in > > > > simple cases, I find myself piping multiple instances of grep instead, > > > > e.g. > > > > > > > > cat file.txt | grep "velo" | grep -v "development" > > > > > > > > Here, "-v" makes grep perform what's called an "inverted matching", that > > > > is, it passes everything through that does NOT match "development". > > > > > > If you mean "velo" as a word on its own, you can use: > > > > > > grep '\<velo\>' file.txt > > > > > > That'll find "velo" with a word boundary on either side. If that's not > > > what you want then I'd just use TF's approach. > > > > That's one I didn't know about, so thanks for the pointer. On further > > research it seems to be specific to grep and not to other regular > > expressions (e.g. Perl doesn't know it). > > Yes it does, but it uses < and > rather than \< and \>. Not in the version of Perl I have (5.10.0 on Snow Leopard). It treats < and > as literal characters, and I can't see any suggestion of those characters being special in perldoc perlre. > Perl only uses backslash before letters in its regular expressions, > punctuation characters that have special meanings are never backslashed. Agreed. -- David Empson dempson(a)actrix.gen.nz
First
|
Prev
|
Pages: 1 2 Prev: Disable/disconnect laptop keyboard (NON-mechanically)? Next: Mac OS X Server 1.0, 1.2 |