LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2007, 05:43 PM   #1
philosophia
Member
 
Registered: Jun 2006
Posts: 54

Rep: Reputation: 15
Need help with awk function


I'm troubleshooting an awk script that parses a text file. This awk function from the script does not seem to work reliably every time:


Code:
function GetEmail(s) {


res=gensub("^.*[^a-zA-Z0-9._-]([a-zA-Z0-9._-]*@[a-zA-Z0-9.-]*)[^a-zA-Z0-9.-].*$", "\\1", "g", s);
        if (res !~ ".*@.*") res="";
        return tolower(res);
}
This function is used to extract strings of email addresses. It fails to extract the first email address from

Code:
'members': {'tav@domain.com': 0, 'cclo@domain.com': 0},
I'm guessing that there is a problem with the regex in the functions that makes it not pick up the first address above.

Sometimes it works perfectly, sometimes it misses email addresses. Sometimes the 'members' string is encoded with more than 2 emails in the brackets.

Any advice on why the function seems to sporadically fail would be appreciated.
 
Old 02-20-2007, 05:53 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not knowing what you feed into s it's hard to assess ... but something like
Code:
{
  for(i=1;i<=NF;i++){
    res=gensub("^.*[^a-zA-Z0-9._-]([a-zA-Z0-9._-]*@[a-zA-Z0-9.-]*)[^a-zA-Z0-9.-].*$", "\\1", "g", $i);
    if (res !~ ".*@.*") res="";
    print res
  }
}
run against the line
Code:
'members': {'tav@domain.com': 0, 'cclo@domain.com': 0},
gives the result
Code:
awk -f mail.awk odd_mails.txt 

tav@domain.com

cclo@domain.com
which looks quite sane to me ....


Cheers,
Tink
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
How do I run a function within a function arguement? C/C++ RHLinuxGUY Programming 12 05-19-2006 02:29 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration