Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2016-10-26 15:57 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (344 votes). Check out past polls.