Ask a Question related to PERL Beginners, Design and Development.
-
Tor Hildrum #1
Re: How to replace a long string in a text file?
perlwannabe wrote:
From the script under you are just replacing lines with a static> 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:
line/value.
<snip>
I don't see any reason to use hex.> 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.
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
-
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... -
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... -
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... -
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... -
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... -
Bob Showalter #2
RE: How to replace a long string in a text file?
perlwannabe wrote:
placey."> 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$!";> _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!You don't need the hex business (although it will work). Assuming your> # 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 $_;
> }
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.
How so?>
>
> 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.
$string = "Here is text\twith\n\nnewlines and tabs\tand spaces.\n";
Bob Showalter Guest
-
Zentara #3
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:
#are you using windows?>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! $!";
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



Reply With Quote

