Professional Web Applications Themes

help in find and replacing a string in a number of files - PERL Beginners

Hi, i am new to perl.I am in a problem,So please help me to get a solution "I want to change texts in 172 files available at a directory /pages(there are files also in a sub-directory),they all are text files. There are also some text files,which I no need to change.I want one single script to open all these files,match the particular string and replace them with new string. I tried with a singel file,able to open that file,make changes ,but only write in another file.I want to write the changes in same old file. the string needed to find: ...

  1. #1

    Default help in find and replacing a string in a number of files

    Hi,
    i am new to perl.I am in a problem,So please help me to get a solution
    "I want to change texts in 172 files available at a directory
    /pages(there are files also in a sub-directory),they all are text files.
    There are also some text files,which I no need to change.I want one
    single script to open all these files,match the particular
    string and replace them with new string.

    I tried with a singel file,able to open that file,make changes ,but only
    write in another file.I want to write the changes in same old file.


    the string needed to find:

    <td bgcolor=#336699 valign=top width=10><img src="a web link here"
    images/corner_2.gif" width="10" height="10"></td>


    the string to be replaced is;

    <td bgcolor=#336699 valign=top width=10>
    <script>
    doent.write("<img src=" + url + "/images/corner_2.gif width='10'
    height='10'></td>");
    </script>
    Can anyone help me in this,
    With Thanks in advance,
    prabu.


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.620 / Virus Database: 399 - Release Date: 3/11/2004



    Prabu Guest

  2. #2

    Default RE: help in find and replacing a string in a number of files

    prabu <co.in> wrote:
    :
    : i am new to perl.I am in a problem,So please help me to
    : get a solution
    : "I want to change texts in 172 files available at a directory
    : /pages(there are files also in a sub-directory),they all are
    : text files.
    : There are also some text files,which I no need to
    : change.I want one
    : single script to open all these files,match the particular
    : string and replace them with new string.

    Read the file 'perlrun' included in your perl distribution.
    The section under the -i option gives examples of how to do this.

    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328

    Charles Guest

  3. #3

    Default RE: help in find and replacing a string in a number of files

    Hi,
    I want to open all (.txt,.html,.js files) files in a particular directory and replace a text in all that files.
    What I done so far was a script to replace text in all files in the current directory.
    What I want was," When I run the script,it should ask for the path of a particular directory and also the
    types(all files of its type in that dir) of files to open.

    I wrote a script to change the directory to I specified through commandline.but again it lists only
    the files in current directory.
    Please help me in this issue,
    The code is

    print "Enter a path name: ";
    my $path=<STDIN>;
    chdir($path);

    my $file='';
    my files=<*.txt>;

    foreach $file (files){
    {
    inner loop for pattern matchinh
    };


    With Thanks inadvance,
    Prabu.



    Win an evening with the Indian cricket captain: Yahoo! India Promos.
    Senthil Guest

  4. #4

    Default RE: help in find and replacing a string in a number of files

    senthil prabu <co.in> wrote:

    [Please post replies to the list and do not top post]

    :
    : "Charles K. Clarkson" <net> wrote:
    : :
    : : Read the file 'perlrun' included in your perl
    : : distribution. The section under the -i option gives
    : : examples of how to do this.
    :
    : Thanks for the reply. Since I am new to perl, I don't
    : know more about perl distribution.

    From the command line type: perldoc perldoc


    : Please help me to get into it or give some sample
    : scripts.

    You can:
    - look for the 'perlrun' file at perldoc.com,
    - Use local html files if you have an ActiveState
    install, or
    - From the command line type: perldoc perlrun


    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328

    Charles Guest

  5. #5

    Default RE: help in find and replacing a string in a number of files



    -----Original Message-----
    From: senthil prabu [mailto:co.in]
    Sent: Friday, April 02, 2004 12:55 AM
    To: Charles K. Clarkson; org
    Subject: RE: help in find and replacing a string in a number of files


    Hi,
    I want to open all (.txt,.html,.js files) files in a particular directory and replace a text in all that files.
    What I done so far was a script to replace text in all files in the current directory.
    What I want was," When I run the script,it should ask for the path of a particular directory and also the
    types(all files of its type in that dir) of files to open.

    I wrote a script to change the directory to I specified through commandline.but again it lists only
    the files in current directory.
    Please help me in this issue,
    The code is

    print "Enter a path name: ";
    my $path=<STDIN>;
    chdir($path);

    my $file='';
    my files=<*.txt>;

    foreach $file (files){
    {
    inner loop for pattern matchinh
    };


    With Thanks inadvance,
    Prabu.

    Senthil,
    Try this in inner loop :
    my files = <*>;
    if ( ! -d $file && $file =~ /\.txt/ )
    {do replacement}
    else { chdir($file) ; do recuresion that calls this function }

    Jay
    Jayakumar Guest

Similar Threads

  1. problem with string replacing
    By Paul Meijer in forum PHP Development
    Replies: 6
    Last Post: January 14th, 01:00 PM
  2. Replacing html Text in a string
    By action bolt webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 1
    Last Post: October 7th, 09:22 AM
  3. find string in files
    By lallous in forum SCO
    Replies: 4
    Last Post: August 6th, 12:57 AM
  4. find string between files
    By e.group in forum Sun Solaris
    Replies: 1
    Last Post: August 3rd, 11:17 AM
  5. Replacing a number string with pictures??
    By Grant in forum Macromedia Dreamweaver
    Replies: 4
    Last Post: July 31st, 06:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139