Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

You could also use perl, which should support \b on all platforms. Assuming your list of replacements is in the format you show (separated by ->), you could do:

perl -F"->" -ane 'chomp;$rep{$F[0]}=${$F[1]}; 
                  END{open(A,"file"); 
                    while(<A>){
                        s/\b$_\b/$rep{$_}/g for keys(%rep); 
                        print
                    }
                  }' replacements

###Explanation

  • The -a makes perl run like awk, automatically splitting fields into the array @F so $F[0] is the 1st field, $F[1] the second etc. The -F sets the input field separator, just like -F in awk. The -n means "read the input file, line by line and apply the script given by -e to each line".

  • chomp : removes newlines (\n) from the end of the line.

  • $rep{$F[0]}=${$F[1]}; : this populates the hash %rep making the pattern to be replaced (the first field, $F[0]) the key and the replacement ($F[1]) the value. * END{} : this is executed after the input file (replacements) has been read.

  • open(A,"file") : open the file file for reading with filehandle A.

  • while (<A>) : read the file line by line.

  • s/// for keys(%rep) : this will iterate through all the keys of the %rep hash, saving each key as the special variable $_. The s/// is the substitution operator and is making the same substitution as explained in Michael's answerMichael's answer.


You could also read through the file and use sed as shown in the other answers:

$ sed 's/->/\t/' replacements | 
    while IFS=$'\t' read from to; do sed -i "s/\b$from\b/$to/g" file; done

You could also use perl, which should support \b on all platforms. Assuming your list of replacements is in the format you show (separated by ->), you could do:

perl -F"->" -ane 'chomp;$rep{$F[0]}=${$F[1]}; 
                  END{open(A,"file"); 
                    while(<A>){
                        s/\b$_\b/$rep{$_}/g for keys(%rep); 
                        print
                    }
                  }' replacements

###Explanation

  • The -a makes perl run like awk, automatically splitting fields into the array @F so $F[0] is the 1st field, $F[1] the second etc. The -F sets the input field separator, just like -F in awk. The -n means "read the input file, line by line and apply the script given by -e to each line".

  • chomp : removes newlines (\n) from the end of the line.

  • $rep{$F[0]}=${$F[1]}; : this populates the hash %rep making the pattern to be replaced (the first field, $F[0]) the key and the replacement ($F[1]) the value. * END{} : this is executed after the input file (replacements) has been read.

  • open(A,"file") : open the file file for reading with filehandle A.

  • while (<A>) : read the file line by line.

  • s/// for keys(%rep) : this will iterate through all the keys of the %rep hash, saving each key as the special variable $_. The s/// is the substitution operator and is making the same substitution as explained in Michael's answer.


You could also read through the file and use sed as shown in the other answers:

$ sed 's/->/\t/' replacements | 
    while IFS=$'\t' read from to; do sed -i "s/\b$from\b/$to/g" file; done

You could also use perl, which should support \b on all platforms. Assuming your list of replacements is in the format you show (separated by ->), you could do:

perl -F"->" -ane 'chomp;$rep{$F[0]}=${$F[1]}; 
                  END{open(A,"file"); 
                    while(<A>){
                        s/\b$_\b/$rep{$_}/g for keys(%rep); 
                        print
                    }
                  }' replacements

###Explanation

  • The -a makes perl run like awk, automatically splitting fields into the array @F so $F[0] is the 1st field, $F[1] the second etc. The -F sets the input field separator, just like -F in awk. The -n means "read the input file, line by line and apply the script given by -e to each line".

  • chomp : removes newlines (\n) from the end of the line.

  • $rep{$F[0]}=${$F[1]}; : this populates the hash %rep making the pattern to be replaced (the first field, $F[0]) the key and the replacement ($F[1]) the value. * END{} : this is executed after the input file (replacements) has been read.

  • open(A,"file") : open the file file for reading with filehandle A.

  • while (<A>) : read the file line by line.

  • s/// for keys(%rep) : this will iterate through all the keys of the %rep hash, saving each key as the special variable $_. The s/// is the substitution operator and is making the same substitution as explained in Michael's answer.


You could also read through the file and use sed as shown in the other answers:

$ sed 's/->/\t/' replacements | 
    while IFS=$'\t' read from to; do sed -i "s/\b$from\b/$to/g" file; done
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

You could also use perl, which should support \b on all platforms. Assuming your list of replacements is in the format you show (separated by ->), you could do:

perl -F"->" -ane 'chomp;$rep{$F[0]}=${$F[1]}; 
                  END{open(A,"file"); 
                    while(<A>){
                        s/\b$_\b/$rep{$_}/g for keys(%rep); 
                        print
                    }
                  }' replacements

###Explanation

  • The -a makes perl run like awk, automatically splitting fields into the array @F so $F[0] is the 1st field, $F[1] the second etc. The -F sets the input field separator, just like -F in awk. The -n means "read the input file, line by line and apply the script given by -e to each line".

  • chomp : removes newlines (\n) from the end of the line.

  • $rep{$F[0]}=${$F[1]}; : this populates the hash %rep making the pattern to be replaced (the first field, $F[0]) the key and the replacement ($F[1]) the value. * END{} : this is executed after the input file (replacements) has been read.

  • open(A,"file") : open the file file for reading with filehandle A.

  • while (<A>) : read the file line by line.

  • s/// for keys(%rep) : this will iterate through all the keys of the %rep hash, saving each key as the special variable $_. The s/// is the substitution operator and is making the same substitution as explained in Michael's answer.


You could also read through the file and use sed as shown in the other answers:

$ sed 's/->/\t/' replacements | 
    while IFS=$'\t' read from to; do sed -i "s/\b$from\b/$to/g" file; done