Sed can be a pain in the butt, so thanks to Shooter.net I’ve recently been changing files with perl using the following code:
perl -pi -e 's/search/replace/g' *.text
-p Assumes an input loop around the script. It reads each line of the file
and outputs it after processing
-i Activates in place editing of files
-e Indicates a single lines script
's/search/replace/g' is the script or command. In this case it's a search and replace regex
*.text the filename(s) to operate on
Extending this via some digging around I did for searching and replacing recursively through subdirectories, I found this piece of code:
find . -print | egrep "*\.html" | xargs perl -pi -e 's/this/that/g'
This is far cleaner than the old:
for each file in *.text
do
sed 's/search/replace/g' < $file >$file.tempfile
mv $file.tempfile $file
done
(The main limitation with sed being that it is unable to output directly to the file you are working on!)
Recent Comments