Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2018-03-19 04:12 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (232 votes). Check out past polls.