SPUG: Inspired, broken.

Tim Maher tim at consultix-inc.com
Wed Feb 9 13:11:24 PST 2005


On Wed, Feb 09, 2005 at 12:52:47PM -0800, Atom Powers wrote:
> Inspired by last night's presentation at the Seattle Unix Group I put 

Glad to hear that! 8-}

> together this perl one-liner to grab email addresses out of a file.
> It works, but I need to save the output to a file, "recipients.txt", and 
> I'm having trouble.
> What is the best way to save the output to a file?
> 
> #perl5 -wnl -e 'm/\w+@\w+\.\w{3}/ and print lc($&)." OK";' address_list.txt
> 
> I've tried this:
> >perl5 -wnl -e 'm/\w+@\w+\.\w{3}/ and print lc($&)." OK";' address_list.txt > recipients.txt
> recipients.txt: Permission denied.

That format /will/ work, assuming you're in a directory for which
you have write permission when you issue the command.

As a general rule, you should backslash the @ within the matching
operator's // delimiters, because it will in some cases be taken
for the start of an array name otherwise.  And double-quotes make
string construction easier:

BEFORE:
> #perl5 -wnl -e 'm/\w+@\w+\.\w{3}/ and print lc($&)." OK";' address_list.txt
 
AFTER:
> #perl5 -wnl -e 'm/\w+\@\w+\.\w{3}/ and print "\L$&\E OK";' address_list.txt

By the way, does that #perl mean that you were running the command
as root?  I wouldn't recommend that until you are more sure about
what you're doing!

> And this:
> >perl5 -wnl -e 'm/\w+@\w+\.\w{3}/ and print { ">recipients" } lc($&)." 
> OK";' address_list.txt

That's a good guess for how Perl might work for a guy who knows AWK,
but it doesn't work that way in Perl.  The output redirection approach
is by far the easiest, so cd to an appropriate directory, and use that
first command format again.

-Tim 
*--------------------------------------------------------------------------*
| Tim Maher, PhD     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
|      Watch for my upcoming book: "Minimal Perl for UNIX/Linux People"    |
*--------------------------------------------------------------------------*


More information about the spug-list mailing list