Thanks to Owen Kaser, I’ve learned that grep can return just the match, and not the whole line.

These examples say it all:

$ grep Ab phone
505-837-2938 Abby Abbott
212-940-2039 Abel Baker
301-302-3030 Abigail Adams

$ grep –o ’Ab[^ ]*’ phone
Abby
Abbott
Abel
Abigail

So, you can get all email addresses in an HTML file using the following syntax (correct me if this is wrong):
grep -o "[a-zA-Z\.]*@[a-zA-Z\.]*" somefile

Under Windows, you can use the Win32 port of grep which has this feature. Tim Charron’s port of grep doesn’t have it. Tim Charron’s version is especially convenient since it supports a findutils-like function (grep -S “pattern” *.txt will go in subdirectories) whereas the findutils port to Windows clashes with the Microsoft find command. (Don’t tell me to use cygwin, I already do.)

Grep would be so much more useful however if it supported reluctant quantifiers.

1 Comment »

  1. Wow, does this mean I don’t need sed anymore?!?

    Comment by Fred — 17/11/2005 @ 4:50

Leave a comment

Warning: When entering a long comment, please ensure that you make copy of your text prior to submitting it. If the server should fail or if you hit a bug, you might lose your work. I am not responsible for your lost effort.

To spammers: I carefully review every single post and make sure that spam gets deleted. You are wasting your time if you are manually entering spam using this form. Read my terms of use to see what I consider to be abusive.

 

« Blog's main page

Powered by WordPress