CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Find & Replace

 



brosskgm
User

Mar 16, 2013, 9:43 PM

Post #1 of 13 (591 views)
Find & Replace Can't Post

I been trying to figure how to get this to work with email address's.

The warning don't seem to like the @ in an email address and calls it an unintended interpolation @gmail in string at -e line 1.

I've tried the following several different ways with no luck.
; in out changed the ' to double quotes.


Code
perl -pi -w -e ...... 

perl -p -i -w -e ....

perl -w -p -i -e 's/$old_email/$new_email/g;' /tmp/*.txt



recruiter
User

Mar 16, 2013, 11:26 PM

Post #2 of 13 (588 views)
Re: [brosskgm] Find & Replace [In reply to] Can't Post

Escape symbol.


Code
perl -pe 's/ [old email] / me\@mail.com /g;' input > *.txt



brosskgm
User

Mar 17, 2013, 5:11 AM

Post #3 of 13 (580 views)
Re: [hwnd] Find & Replace [In reply to] Can't Post

Thanks for your reply.

I already tried that on a command line and it worked but the problem I was having is the "$new_email" is entered in from a form page.

I'll keep working at it. I'll see if I can use str_replace before it's sent to perl.

Thanks for your help.

UPDATE........

str_replace worked. Thanks


(This post was edited by brosskgm on Mar 17, 2013, 7:55 AM)


brosskgm
User

Mar 17, 2013, 12:21 PM

Post #4 of 13 (567 views)
Re: [hwnd] Find & Replace [In reply to] Can't Post

Something I just thought about, is there a way if $old_email is found to delete the complete line?

I've tried a couple things, but trying to get it to work with multiple


Code
perl -i -pe 's/$old_email//s' file



Laurent_R
Enthusiast / Moderator

Mar 17, 2013, 12:48 PM

Post #5 of 13 (562 views)
Re: [brosskgm] Find & Replace [In reply to] Can't Post

perl -i -ne 'print unless /$old_email/' file


recruiter
User

Mar 17, 2013, 12:49 PM

Post #6 of 13 (562 views)
Re: [brosskgm] Find & Replace [In reply to] Can't Post

You could try a few ways of doing this:


Code
  

perl -ni -e 'print unless /pattern/' file

perl -ne 'if( /pattern/ ) { my $f = $_ } elsif( !/pattern/ ) { print $f, $_; $f = "" } else { $f ="" }' file



brosskgm
User

Mar 18, 2013, 4:47 PM

Post #7 of 13 (549 views)
Re: [hwnd] Find & Replace [In reply to] Can't Post

This worked great. Thanks.

Now this created a new issue. It doesn't delete the file if it's empty but that's not the hard part.

When the old_email is deleted there is also an $email_message file.

There is two files that have the same name but different extensions.

If one file is empty I'm having a problem to delete it's sister file also.

Is it possible to get the file name without extension that way I can add the sister extension to also be deleted?

$email_address_file = "$subscr_id\.txt"; // email file
$email_message = "$subscr_id\.msg"; // email message file.


Code
if ( -z "$old_email" ) { 
perl -e '!-s&&unlink $_ for<$old_email>';
print "File Deleted\n";
// Need to remove message file
}
Print "email file not empty, leave them both alone";



Laurent_R
Enthusiast / Moderator

Mar 19, 2013, 9:29 AM

Post #8 of 13 (539 views)
Re: [brosskgm] Find & Replace [In reply to] Can't Post

You may want to take a look at this module:


Code
File::Basename



brosskgm
User

Mar 19, 2013, 10:25 AM

Post #9 of 13 (533 views)
Re: [Laurent_R] Find & Replace [In reply to] Can't Post

Thanks, I have been looking at that.

Still lost. Don't understand how to work this to what I'm trying to do. They all seem to be talking about file path etc.

It's way to advanced for me. All the code I'm seeing they all have the full name hard coded.

Not good with Perl.


Code
use File::Basename; 

$fullname = "/usr/local/src/perl-5.6.1.tar.gz"; // I don't know the full name.

$file = basename($fullname);
# file="perl-5.6.1.tar.gz"

$dir = dirname($fullname);
# dir="/usr/local/src"


($file,$dir,$ext) = fileparse($fullname, qr/\..*/);
# dir="/usr/local/src/" file="perl-5" ext=".6.1.tar.gz"

($file,$dir,$ext) = fileparse($fullname, qr/\.[^.]*/);
# dir="/usr/local/src/" file="perl-5.6.1.tar" ext=".gz"

($file,$dir,$ext) = fileparse($fullname, qr/\.\D.*/);
# dir="/usr/local/src/" file="perl-5.6.1" ext=".tar.gz"

($file,$dir,$bak) = fileparse("/tmp/file.bak",
qr/~+$/, qr/\.(bak|orig|save)/)
# dir="/tmp/" file="file" ext=".bak"

($file,$dir,$bak) = fileparse("/tmp/file~",
qr/~+$/, qr/\.(bak|orig|save)/)
# dir="/tmp/" file="file" ext="~"



Laurent_R
Enthusiast / Moderator

Mar 19, 2013, 12:02 PM

Post #10 of 13 (528 views)
Re: [brosskgm] Find & Replace [In reply to] Can't Post

I am not sure I understood what you want to do, but I had in mind something like this:


Code
my @suffixlist = qw /txt dat pl tmp/; 
my $file_name = "./foo/bar/input.txt";
my ($name, $path, $suffix) = fileparse ($file_name, @suffixlist);
print $name, "\n"; # prints "input."


In other words, you obtain the name of the file whithout file path and extension. Then, you can construct with that the name of the other file which you want to delete.

Or did you want to do something else?


brosskgm
User

Mar 19, 2013, 12:47 PM

Post #11 of 13 (524 views)
Re: [Laurent_R] Find & Replace [In reply to] Can't Post

I'm not sure what that code says at all.

So hard to type this out sometime but let me try.

When the line is deleted with an email address as previous perl -ne etc. that works great!. The file $subscr_id.txt had the email address's in it.

But if the file is empty after the line or lines are deleted I'm trying to figured out how to also delete the message file $subscr_id.msg

$subscr_id.txt and $subscr_id.msg have the same file name, but have different extensions. txt or msg


Code
$email_address_file = "$subscr_id\.txt"; // email file 
$email_message = "$subscr_id\.msg"; // email message file.



Laurent_R
Enthusiast / Moderator

Mar 19, 2013, 2:51 PM

Post #12 of 13 (518 views)
Re: [brosskgm] Find & Replace [In reply to] Can't Post

If you just want to delete the file "subscr_id.msg" if the file "subscr_id.txt" meets a certain condition (say, is empty), you can just do something like this:


Code
my $sub_txt = "subscr_id.txt" ; 
if (-z $sub_txt) { # if file "subscr_id.txt" has zero size
$sub_txt =~ s/\.txt/.msg/; #$sub_txt not contains "subscr_id.msg"
unlink $sub_txt; # deletes "subscr_id.msg"
}



brosskgm
User

Mar 19, 2013, 2:56 PM

Post #13 of 13 (514 views)
Re: [Laurent_R] Find & Replace [In reply to] Can't Post

Thank you so much.

I'll give this a run this evening or tomorrow morning
when I'm back in front of my system.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives