Replacing strings in a file from a def file

I am sure that there is something out there but before I learn Perl I thought I would ask. Here is what I need: I need a script or program to substitute string values in an xml file from a pair definition file

replace_string -d <file containing string pairs> original_file_in new_file_out

The definition file is a text file with pairs

dog cat
elephant baby

running the script on the original file would generate a new file where every occurrence of dog would be replaced by cat and elephant by baby and so on.

Sorry if this is a stupid question, I am rusty.

Takes three arguments from the command line: pair-file input-file output-file

#!/usr/bin/env ksh

awk -v pfile=$1 '
    BEGIN {
        while( (getline<pfile) > 0 )
            list[$1] = $2;
        close( pfile );
    }
    {
        for( x in list )
            gsub( x, list[x], $0 );
        print;
    }

' $2 >$3
exit

Should run in most Bourne like shells

1 Like
$
$ # the definition file "f32.def"
$
$ cat f32.def
dog        cat
elephant   baby
$
$ # the data file "f32"
$
$ cat f32
this line has the word dog in it
this is the second line
this line has one elephant and another elephant in it
and one more elephant here
one dog and another dog and yet another dog here
the last line of this file...
$
$ # the Perl one-liner
$
$ perl -lne '$#ARGV == 0 ? {/^(\w+)\s+(\w+)$/ and $x{$1}=$2} : print join " ", map {$x{$_} // $_} split' f32.def f32
this line has the word cat in it
this is the second line
this line has one baby and another baby in it
and one more baby here
one cat and another cat and yet another cat here
the last line of this file...
$
$

tyler_durden

Works perfectly, exactly what I was looking for. Thanks!