Bulk File Editing


Mass Edit

To edit every file in a given directory and make a change to a line should it appear, is a two line script using a for loop and sed. It's two lines because you have to stream to a new file, then replace the old Below are the individual commands and the combo

The example below is something I have to do with awstats every once in a while when the system date was off during an update.

The sed command is

sed -e "s/^LastUpdate.20060428/LastUpdate 20060322/" /home/stats/somefile

The command to echo eveything in a directory was

for FILE in `ls /home/stats/data/hosts/awstats032006*.txt`; do echo $FILE; done

Put them together like thus:

for FILE in `ls /home/stats/data/hosts/awstats032006*.txt`; do sed -e "s/^LastUpdate.20060428/LastUpdate 20060322/" $FILE > ${FILE}.new; done

for FILE in `ls /home/stats/data/hosts/awstats032006*.txt`; do mv ${FILE}.new $FILE; done