Google Answers Logo
View Question
 
Q: awk: extracting data ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: awk: extracting data
Category: Computers > Programming
Asked by: milesw-ga
List Price: $5.00
Posted: 07 Jun 2002 00:34 PDT
Expires: 14 Jun 2002 00:34 PDT
Question ID: 23491
awk: extracting data

Given a simple address book with poor formatting:

john doe john@domain.com 555-111-1212
sally@domain.co.uk 555-555-1212 sally doe
515-1212   joe blow    joe@domain.info
jane doe     jane@domain.com       bob doe  bob@domain.com
etc...

How can just the email addresses be extracted to a new file using awk
alone?

Please do not answer this question unless you have an answer that
works using only awk.
Answer  
Subject: Re: awk: extracting data
Answered By: bookface-ga on 07 Jun 2002 01:15 PDT
Rated:5 out of 5 stars
 
bash-2.03$ awk '
           {
              for(i=1;i<=NF;i++){
                  if($i ~ /@/){
                      print $i
                  }
              }
         }
' awktest
john@domain.com
sally@domain.co.uk
joe@domain.info
jane@domain.com
bob@domain.com

Thank you for using Google! Answers.

Clarification of Answer by bookface-ga on 07 Jun 2002 01:23 PDT
Thought I'd post a few comments to help you better understand the code.

In each line:

for(i=1;i<=NF;i++){ 
   for each field up to the number of fields,
if($i ~ /@/){ 
   if the current field contains an @ sign,
print $i 
   then it's an email address, print it.
milesw-ga rated this answer:5 out of 5 stars
Many, many thanks for the excellent answer and blazing fast response
time. Also appreciated the extra information you kindly added in the
clarification.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy