Solved

Email Validation Help with command line usage in Ubuntu

Posted on 2010-09-05
8
1,000 Views
Last Modified: 2013-12-06
Hi,

I've been searching for a while now for an optimal way to clean up a list of emails and came across two sources that to me seem to provide the best possible solution.

#1) http://www.linuxjournal.com/article/9585?page=0,3
#2) http://php-email-address-validation.googlecode.com/svn/trunk/EmailAddressValidator.php

I chose to try the second one but I'm not quite sure how to use this script on the command line. I run Ubuntu through virtual box and I need some help with how I would run this scrip.

I saved the script (#2) in a local directory as emailvalidator.php and have another file with emails called emailcheck.txt.

What command would I need to use to call this script in the gnome terminal? Thank you in advance



0
Comment
Question by:faithless1
  • 5
  • 2
8 Comments
 
LVL 3

Expert Comment

by:ssapkota
ID: 33607666
If you are searching for a tool in gnome-terminal.
Then I better suggest you to use the power of regular expression from tools like grep/sed/awk.
Eg: if  you have a list of email addresses in a file(one in each line), you can run the following commands to filter the valid ones only.
cat file | grep -P '[a-zA-Z][a-zA-Z0-9\._-]+@[a-zA-Z0-9][a-zA-Z0-9\.]+'

[Note: this is just a quick solution and need some more tweak to perform flawlessly.]

Or if its possible can you share a chunk from your source (can be modified as well), and we can give you a full fledged solution.
Those scripts that you found are useful for php based online solution.
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33607669
If you want to read about perl based regular expression:
http://www.troubleshooters.com/codecorn/littperl/perlreg.htm
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33608111
Here is a teaching example showing how to validate an email address in PHP.  There are many such email validator scripts on the WWW and almost all of them are wrong in one way or another.

Good luck with it, ~Ray
<?php // RAY_email_validation.php
error_reporting(E_ALL);

// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE

function check_valid_email($email)
{
    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    // MAN PAGE: http://us3.php.net/manual/en/intro.filter.php
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }
    else
    {
        $regex = '/^[A-Z0-9_-][A-Z0-9._-]*@([A-Z0-9][A-Z0-9-]*\.)+[A-Z\.]{2,6}$/i';
        if (!preg_match($regex, $email)) return FALSE;
    }

    // FILTER or PREG DOES NOT TEST IF THE DOMAIN OF THE EMAIL ADDRESS IS ROUTABLE
    $domain = explode('@', $email);
    if ( checkdnsrr($domain[1],"MX") || checkdnsrr($domain[1],"A") ) return TRUE;

    // EMAIL NOT ROUTABLE
    return FALSE;
}

// DEMONSTRATE THE FUNCTION IN ACTION
$e = '';
if (!empty($_GET["e"]))
{
    $e = $_GET["e"];
    if (check_valid_email($e))
    {
        echo "<br/>VALID: $e \n";
    } else
    {
        echo "<br/>BOGUS: $e \n";
    }
}
// END OF PHP - PUT UP THE FORM
?>
<form method="get">
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="<?php echo $e; ?>" />
<input type="submit" />
</form>

Open in new window

0
 

Author Comment

by:faithless1
ID: 33608167
Thanks for the responses.

- Ray, how would I run this script on the command line in unix?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33608176
Sorry - I do not know.  I do not use the command line; I use PHP scripts.  Since this was posted in the PHP zone I could only help with a PHP script.  Maybe one of the other experts could show you how to adapt it to command-line execution.  I am sure it is possible.
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33609048
If you want to execute those php scripts, run:
$ php /path/to/file.php
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33609055
0
 
LVL 3

Expert Comment

by:ssapkota
ID: 33644777
@faithless1, Its not fair. The answer you accepted doesn't answer your question. Its just a simpler version of what you obtained yourself in #2.
My last 2 posts are directly answering your question.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now