How to replace a long string in a text file?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Re: How to replace a long string in a text file?

    perlwannabe wrote:
    > Now that I have, with all of you helping, dealt with the unlink problem.
    > I am now faced with a new problem.
    >
    > I have a file, myfile.txt, that consists of various text items that are
    > repetitive. I am trying to replace many of the repetitive items with a
    > place holder. Here is an example of myfile.txt:
    From the script under you are just replacing lines with a static
    line/value.

    <snip>
    > Perhaps I am attacking this problem wrong by using hex, but it seems
    > easier to use then text which has carriage returns, tabs, and spaces.
    I don't see any reason to use hex.
    There is a common idiom to use when searching for repetitions/uniqueness.
    ## untested example :)

    @array = qw(one two one three four);

    for (@array) {
    if (defined($unique{$_}) {
    s/$_/replacement/;
    } else {
    $unique{$_}++; ## $unique{$_} is now defined and true.
    } ## and will be replaced if it comes up again

    This works equally well for files, it's just a matter of s/@array/<FH>/
    There *might* be a question of memory usage though, depending on
    the size of the file your working with.

    Also, sort -u would be faster and more efficient if you have large files,
    but didn't need the replacement text.

    Tor

    Tor Hildrum Guest

  2. Similar Questions and Discussions

    1. search and replace in complexed text file?
      I have a search and replace task and was hoping to get some advice. I have a large text file of remote access users from Cisco's ACS dumpfile that...
    2. replace string in file
      Hi! I am using PHP4.3.3 on a windows XP platform. I am building a small program that will allow my students to subscribe and unsubscribe from...
    3. How to replace a variable string within /* variable_string */ with x for each character in string?
      How to replace a variable string within /* variable_string */ with x for each character in string? The string may span on multiple lines. for...
    4. Error when updating a long text string
      Hi I have a windows form app which uses a webservice to update data using datasets. On one occasion the dataset contains a string value who's...
    5. How to replace a text in a file
      On Aug 5, Vinay Thombre said: I hope you've got a good book or two, such as "Learning Perl" (the llama book) published by O'Reilly. The...
  3. #2

    Default RE: How to replace a long string in a text file?

    perlwannabe wrote:
    > Now that I have, with all of you helping, dealt with the unlink problem.
    > I am now faced with a new problem.
    >
    > I have a file, myfile.txt, that consists of various text items that are
    > repetitive. I am trying to replace many of the repetitive items with a
    > place holder. Here is an example of myfile.txt:
    >
    > This is a long file with sentences, paragraphs and returns. Certain
    > portions of the file never change and are simply repetitive. I would like
    > to replace all of the repetitive items with a placeholder...call it
    placey."
    > _END FILE_
    >
    > I would like to replace "This ... items" with "placey" so the above would
    > look like:
    >
    > placey with a placeholder...call it "placey."
    >
    > When attacking this problem I thought it would be best to use hex values
    > so as not to deal with returns, etc. myfile.txt in hex is:
    >
    > 54 68 69 73 20 69 73 20 61 20 6C 6F 6E 67 20 66 69 6C 65 20 77 69 74 68 20
    > 73 65 6E 74 65 6E 63 65 73 2C 20 70 61 72 61 67 72 61 70 68 73 20 61 6E 64
    > 20 72 65 74 75 72 6E 73 2E 20 20 43 65 72 74 61 69 6E 20 70 6F 72 74 69 6F
    > 6E 73 0D 0A 6F 66 20 74 68 65 20 66 69 6C 65 20 6E 65 76 65 72 20 63 68 61
    > 6E 67 65 20 61 6E 64 20 61 72 65 20 73 69 6D 70 6C 79 20 72 65 70 65 74 69
    > 74 69 76 65 2E 20 20 49 20 77 6F 75 6C 64 20 6C 69 6B 65 20 74 6F 20 72 65
    > 70 6C 61 63 65 20 61 6C 6C 0D 0A 6F 66 20 74 68 65 20 72 65 70 65 74 69 74
    > 69 76 65 20 69 74 65 6D 73 20 77 69 74 68 20 61 20 70 6C 61 63 65 68 6F 6C
    > 64 65 72 2E 2E 2E 63 61 6C 6C 20 69 74 20 22 70 6C 61 63 65 79 2E 22
    >
    > Here is the little script that I am working with and, obviously, is not
    > working:
    >
    > my $filefirst = "c:/perl/myfile.txt";
    > open(FILE,"<$filefirst") || die "Could not open file for reading! $!";
    > open(TEMP,">$filefirst.tmp") || die "Could not open file for writing!
    $!";
    > # I have used the . . . to show that the entire hex string is really there
    > my $hextostr = '\x54\x68\x69\x73 . . . \x79\x2E\x22';
    > while(<FILE>){
    > $_ =~ s/$hextostr/placey/gi;
    > print TEMP $_;
    > }
    You don't need the hex business (although it will work). Assuming your
    long literal is in a variable $string, just:

    s/\Q$string/placey/gi;

    will work (do you need /i?)

    The problem appears to be that you are reading the file line by line,
    but want to search and replace over multiple lines. If you set $/ to
    undef, you can read the whole file into a single string and perform
    the replacement that way.
    >
    >
    > Perhaps I am attacking this problem wrong by using hex, but it seems
    > easier to use then text which has carriage returns, tabs, and spaces.
    How so?

    $string = "Here is text\twith\n\nnewlines and tabs\tand spaces.\n";
    Bob Showalter Guest

  4. #3

    Default Re: How to replace a long string in a text file?

    On Tue, 12 Aug 2003 01:29:39 -0400 (EDT), [email]perlwannabe@antigone.cotse.net[/email]
    (Perlwannabe) wrote:
    >Here is the little script that I am working with and, obviously, is not
    >working:
    >
    >my $filefirst = "c:/perl/myfile.txt";
    >open(FILE,"<$filefirst") || die "Could not open file for reading! $!";
    >open(TEMP,">$filefirst.tmp") || die "Could not open file for writing! $!";
    #are you using windows?
    binmode FILE; #need binmode under windows
    binmode TEMP;
    ># I have used the . . . to show that the entire hex string is really there
    >my $hextostr = '\x54\x68\x69\x73 . . . \x79\x2E\x22';
    >while(<FILE>){
    > $_ =~ s/$hextostr/placey/gi;
    > print TEMP $_;
    >}
    >
    >
    >Perhaps I am attacking this problem wrong by using hex, but it seems
    >easier to use then text which has carriage returns, tabs, and spaces.
    >
    >I hope this explains the situation.
    >
    Zentara Guest

Posting Permissions

  • You may not post new threads
  • You may 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