automate file text edits
Assuming all files are in the same folder:
mkdir new
for i in *.html; do
sed 's|website.com|website.com/old|g' "$i" > "new/$i"
done
If you're commonly editing website file contents you should invest in an editor.
Sublime Text 2
Drop the website onto the icon and it will build the project:
run cmdshiftF and enter the replacement:
Click the Replace
button and verify the Replace
:
It will open and change all the files:
BBEdit
Is an excellent and robust application I primarily use to edit code. Drag and drop the folder onto BBEdit and go to File -> Save Project
and it will create a file named foobar.bbprojectd
:
run cmdshiftF and select the project with the replacement parameters:
It will prompt you after clicking Replace All
:
If you are unsure on your changes select Leave Open
because if you select Save to disk
it is very hard to revert back.
Perl
There is already a sed
solution but you could also do the same with Perl:
# !/bin/bash
DIR="/Users/vader/desktop/test"
NEW="$DIR/new"
if [ ! -e "$DIR/new" ]; then
mkdir -p new
echo "made folder new"
else
echo "folder exists"
fi
for htmlfile in $(ls "$DIR"/*.html); do
TEMPFILE="$htmlfile.$$"
htmlbase=$(basename $htmlfile)
echo "Working on "$htmlbase
perl -pe 's|www.website.com|www.website.com\/old|g' "$htmlfile" > $TEMPFILE && mv $TEMPFILE "$NEW/$htmlbase"
done
There are other editors out there but these are the two I primarily use. You could also do this in pure AppleScript but you haven't shown any code and mentioned terminal so I made the above in Perl.