Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 about the Monastery: (13)
As of 2015-07-01 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (16 votes), past polls