2

I'm having trouble using the following code inside my Perl script, any advise is really appreciated, how to correct the syntax?

# If I execute in bash, it's working just fine

bash$ whois google.com | egrep "\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}" |awk ' {for (i=1;i<=NF;i++) {if ( $i ~ /[[:alpha:]]@[[:alpha:]]/ )  { print $i}}}'|head -n1

[email protected]

#-----------------------------------

#but this doesn't work 

bash$ ./email.pl google.com
awk:  {for (i=1;i<=NF;i++) {if (  ~ /[[:alpha:]]@[[:alpha:]]/ )  { print }}}
awk:                              ^ syntax error

# Here is my script
bash$ cat email.pl 
####\#!/usr/bin/perl         


$input = lc shift @ARGV;

$host = $input;

my $email = `whois $host | egrep "\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}" |awk ' {for (i=1;i<=NF;i++) {if ( $i ~ /[[:alpha:]]@[[:alpha:]]/ )  { print $i}}}'|head -1`;
print my $email;

bash$
2
  • 1
    You shouldn't do this, but since you are, you forgot to escape the $ in $i. (Next time, read the error message more carefully, and you won't have to ask SO.) Commented Apr 19, 2010 at 4:31
  • 1
    Also, Perl was created to replace grep/sed/awk/... so you should just read the whois output and parse it in Perl. Commented Apr 19, 2010 at 13:19

3 Answers 3

4

use a module such as Net::Whois if you want to code in Perl. Search CPAN for more such modules dealing with networking. If you want to use just Perl without a module, you can try this (note you don't have to use egrep/awk anymore, since Perl has its own grepping and string manipulation facilities )

   open(WHOIS, "whois google.com |")    || die "can't fork whois: $!";
   while (<WHOIS>) {

       print "--> $_\n";  # do something to with regex to get your email address
   }            
   close(WHOISE)                      || die "can't close whois: $!";
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest (though not the smoothest) way to use awk inside Perl is a2p.

echo 'your awk script' | a2p

Comments

0

As mentioned by others, backticks interpolate, so its tripping on the $'s. You could escape them all, or you could use single quotes like so:

open my $pipe, "-|", q[whois google.com | egrep ... |head -n1];
my $result = join "", <$pipe>;
close $pipe;

This takes advantage of open's ability to open a pipe. The -| indicates the filehandle $pipe should be attached to the output of the command. The chief advantage here is you can choose your quoting type, q[] is equivalent to single-quotes and not interpolated so you don't have to escape things.

But oh god, pasting an awk script into Perl is kind of silly and brittle. Either use a module like Net::Whois, or do the whois scraping in Perl, possibly taking advantage of things like Email::Find, or just write it as a bash script. Your Perl script isn't doing much in Perl as it stands.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.