howto replace strings in more than one file at once


Author: Udo Rader

last modification: 2003-08-11

Imagine a situation where you have a couple of files in a certain directory that contain a certain string that you need to replace by another.

Does not sound too difficult? It actually isn't, you could easily do this by hand you think? Yes, that may be fine, if you only have 5 files, but what if you have 50 and they need to be modified frequently? Well, then manual editing will probably be not too funny ;-)

The solution is to use sed in a small shell script:

#!/bin/bash
# replaces a pattern within a file by another
# udo, jul 17 03

# change to whatever you need
OLD='the old string'
NEW='the wonderful new string'
FILES_TO_CHANGE='the_files_to_change*.pl'

for ORIGINAL in ${FILES_TO_CHANGE} ; do
    # -i        ... modify file on the fly
    # "1 s/..." ... substitute only on the first line
    sed -i -e "1 s/${OLD}/${NEW}/" ${ORIGINAL}
done

The script replaces OLD by NEW in all files defined as FILES_TO_CHANGE.

Of course this could be more sophisticated (eg. by getting its parameters via commandline), but thats left to you, I live happily and peacefully the way it is.
consulting IT Eclipse RCP eclipse rcp Eclipse-RCP tirol innsbruck österreich tyrol austria europa europeSoftware, Development, Softwareentwicklung, programming, Programmierung, SE, SD, Tirol, Tyrol, Innsbruck, Austria, Österreich, Europa, EuropeSystemadministration, Linux, Tirol, Sysadmin, Netzwerke, network, Linux, Windows, migration, tirol, tyrol, innsbruck, Österreich austria Europa, EuropeIT-Sicherheit, IT-Security, security network, EDV-Sicherheit, Netzwerksicherheit, tirol, tyrol innsbruck, Österreich Austria Europa, EuropeHosting, combeso, typo3, xims, Linux, server, tirol, tyrol innsbruck, Österreich, Austria Europa, EuropeLinux, Open Source, OSS, UNIX, Windows, tirol, tyrol innsbruck, Österreich, Austria, Europa, EuropeBISCAT, BiSCAT, Personenverwaltung, client administration tool, Mitarbeiterverwaltung, staff, Österreich, Austria, tirol innsbruck Europa, EuropedrawSWF, flash, grafiken, graphics, erstellung, development, tirol tyrol, innsbruck, Österreich, Austria, Europa, Europehomepage, webshop, günstig, easy cheap, umfangreich, comprehensive, tirol tyrol, innsbruck, Österreich, Austria, Europa, Europezooners die eventplattform, Orte, Veranstaltungen, Wien, Landeck, Tirol, Munich, Germany, Austria, zooncards, zooncard