Search & replace string in multiple files
Posted: Fri Dec 15, 2006 1:00 pm
I had to replace IPs within several m3u files by a hostname.
Thanks to the info on this site, I had the one-liner that does the trick:
I've used this regexp for matching any IP:
I've slightly enhanced it a bit for being reusable by others on our machine, too:
NOTE: I had to replace the single-quotes by double quotes to use bash variables.
Thanks to the info on this site, I had the one-liner that does the trick:
Code: Select all
find . -name '*.txt' |xargs perl -pi -e 's/find/replace/g'
Code: Select all
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Code: Select all
#!/bin/bash
#
# replaces a certain pattern in multiple files.
# @author: ^Rooker
# @date: Dec 15th, 2006
FILEMASK="*.m3u";
HOSTNAME="www.das-werkstatt.com";
case "$1" in
GO)
find . -name "$FILEMASK" | xargs perl -pi -e "s/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/$HOSTNAME/g"
;;
LIST)
find . -name "$FILEMASK"
;;
*)
echo;
echo "Replaces any IP within $FILEMASK files by a given hostname ($HOSTNAME)"
echo "SYNTAX: $0 (GO|LIST)";
echo;
;;
esac