Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression problem.

Status
Not open for further replies.

linuxMaestro

Instructor
Jan 12, 2004
183
US
This matches everything after the from:
/^From:(.*)/"
Ex. "Name Here" <email@domain.com>"

But I am trying to just match the email, I tried all of the following, but none work:
/^From:(\<+.*+\>)/"
/^From:+.*+(<+.*+>)/"
/^From:+.*+(\<+.*+\>)/"
/^From:+(.*+(\<+.*+\>))/"
/^From:+(.*+\<+.*+\>)/"
/^From:+(.*+(<+.*+>))/"
/^From:+(.*+<+.*+>)/"

What am I doing wrong?
/^From: start with From:
+.* match 0 or more characters before
(\<+.*+\>) 0 or more characters enclosed in angle brackets before
/" End of line

A little help please.
 
Code:
/^From:/ { match($0, "<.*>"); printf("[%s]\n", substr($0, RSTART+1, RLENGTH-1))}

or am I missing something?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Regular expressions in Awk are not quite the same as those in Perl.

To match a line that starts with [tt] From: [/tt] and that ends with [tt] <xxxx@xxxx.xxx> [/tt]:
Code:
match( $0, /^From: .*<.*>$/ )
If Awk had "capturing", we could extract the email address with:
Code:
match( $0, /^From: .*<(.*)>$/ )
But it doesn't. So I would use this destructive method:
Code:
if( gsub( /^From: .*<|>$/, "" )) print
else print "No email found in line", FNR
If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.

For an introduction to Awk, see faq271-5564.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top