Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: search and replace with a temp file at a certain substr position

by ikegami (Patriarch)
on Jul 20, 2005 at 19:34 UTC ( [id://476598]=note: print w/replies, xml ) Need Help??


in reply to search and replace with a temp file at a certain substr position

my $gpg = '/home/gpghrp/.gnupg'; my $file = "$gpg/scripts/gpg_encr.saved"; my $temp = "$file.new"; open(OLD, '<', $file) or die "Can't open $file: $!\n"; open(NEW, '>', $temp) or die "Can't create $temp: $!\n"; while (<OLD>) { # Faster than regexp for plain text. if (index($_, '(?)readonly tlx=') >= 0) { substr($_, 13, 8, "blah"); } print NEW $_; } close(NEW); close(OLD); rename($temp, $file) or die "Can't update $file: $!\n";

or as a one-liner:

perl -i -pe '/\Q(?)readonly tlx=\E/ && substr($_,13,8,"blah");' /home/ +gpghrp/.gnupg/scripts/gpg_encr.saved

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://476598]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found