Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to replace strings in a file from a list?

by Kenosis (Priest)
on Oct 03, 2012 at 03:49 UTC ( #996973=note: print w/ replies, xml ) Need Help??


in reply to How to replace strings in a file from a list?

Here's another option:

use strict; use warnings; use File::Slurp qw/ read_file write_file /; $ARGV[0] or die "\nUsage: $0 <File>\n\n"; my $userlistContents = read_file $ARGV[0]; for ( grep /\S/, <DATA> ) { my ( $before, $after ) = split; $userlistContents =~ s/\b$before\b/$after/g; } write_file "changed_$ARGV[0]", $userlistContents; __DATA__ JOE JOHN FRED FREDERICK SAM SAMANTHA

Using File::Slurp is a nice way to get the file's contents into a scalar upon which global substitutions can be performed. The before and after names in the __DATA__ area are tab separated. Note that the changed file contents are written to the file named "changed_$ARGV[0]".

Hope this helps!


Comment on Re: How to replace strings in a file from a list?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2014-11-29 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (204 votes), past polls