how to grep strings containing 2 or more periods

how to grep strings containing 2 or more periods

Post by rachelms7 » Sat, 16 Apr 2005 03:37:41


How do I grep a string with 2 or more periods? I'm trying to grep out
URL's from
a text file, where some URLs might be written as full
http://www.yqcomputer.com/ but some might be just blah.blah.com

I tried grep ".\.\{2,\}" but it doesn't work - I guess I need to allow
any number of characters in the string amongst any number of periods
(>2).

Thanks.
 
 
 

how to grep strings containing 2 or more periods

Post by Ed Morto » Sat, 16 Apr 2005 04:00:55


grep "\..*\." file

Ed.

 
 
 

how to grep strings containing 2 or more periods

Post by Jeremiah D » Sat, 16 Apr 2005 04:01:57


In general, '\..*\.' will do it. Remember, '.' is a regex
metacharacter, so you have to escape it if you want a literal '.', you
have to escape it. However, that will also match '..', so you might
want to refine it a bit more. The regex '[^.]+' matches at least one
non-dot character, so you could put that in, like '\.[^.]+\.' Even that
might not work to catch URLs, because it will match ".foo."

To really match URLs in the most generic sense is close to impossible,
because
a) URLs don't have to be fully qualified - 'kaffaljidhma' might be a valid
URL for me but not for you, or vice versa
b) we can't know how clean your original text is - regular English prose
is unlikely to contain sequences like '.foo.', but if your original text
contains things like filenames, there's no telling what might be in it.

JDW
 
 
 

how to grep strings containing 2 or more periods

Post by Michael To » Sat, 16 Apr 2005 04:36:52


Your regex searches for two or more consecutive periods.

grep -i "[a-z][-a-z0-9]\{1,\}\.[a-z]\{2,\}\.[a-z]\{2,\}"

searches for two periods surrounded by some characters.

--
Michael Tosch @ hp : com