LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find & replace in multiple files (https://www.linuxquestions.org/questions/linux-newbie-8/find-and-replace-in-multiple-files-4175458901/)

chonk0 04-20-2013 10:20 AM

find & replace in multiple files
 
How can I search all *.html files in a directory and it's sub directories for the word "p3nor" and replace them with "p3nis"?

Madhu Desai 04-20-2013 11:56 AM

See this: http://www.linuxquestions.org/questi...ng-4175453776/

PTrenholme 04-20-2013 01:26 PM

If you use the KDE desktop manager, install the krename package and see if that can solve your problem. (GNOME may have a similar package but I don't use GNOME enough to know it's name.)

Also, you could look at the various -exec options of the find command.

chonk0 04-20-2013 02:50 PM

thanks for the replies. I found a script on another forum that would do what I'm after:


# *****************************************************************************************
# find_and_replace_in_files.sh
# This script does a recursive, case sensitive directory search and replace of files
# To make a case insensitive search replace, use the -i switch in the grep call
# uses a startdirectory parameter so that you can run it outside of specified directory - else this script will modify itself!
# *****************************************************************************************

!/bin/bash
# **************** Change Variables Here ************
startdirectory="/your/start/directory"
searchterm="test"
replaceterm="test=ok!"
# **********************************************************

echo "***************************************************"
echo "* Search and Replace in Files Version 01-Aug-2012 *"
echo "***************************************************"

i=0;

for file in $(grep -l -R $searchterm $startdirectory)
do
cp $file $file.bak
sed -e "s/$searchterm/$replaceterm/ig" $file > tempfile.tmp
mv tempfile.tmp $file

let i++;

echo "Modified: " $file
done

echo " *** All Done! *** Modified files:" $i

David the H. 04-21-2013 04:36 PM

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

That's not a very good script. In particular it relies on the Don't Read Lines With For error. None of the variables are quoted either, the $i variable is never even used, and you have to hard-code the values into the script.

Code:

#!/bin/bash
# **************** Change Variables Here ************
# Alered to use the first 3 arguments by default,
# but will fall back to the entered values if not supplied.

startdirectory="${1:-/your/start/directory}"
searchterm="${2:-test}"
replaceterm="${3:test=ok!}"
# ***************************************************

while IFS='' read -rd '' fname; do

    sed -i "/$searchterm/ s/$searchterm/$replaceterm/ig" "$fname"
    echo "Modified: $fname"

done < <( grep -R -Z -l "$searchterm" "$startdirectory" )

exit 0



All times are GMT -5. The time now is 10:55 AM.