Read, Write and Append -- is there more?
Hello, I am currently frustrated trying to do a simple find and replace on a file, but the three open options: Read, Write and Append don't seem to give me what I want. For example, Read, works on the finding, but nothing will replace. Write erases all the files and leaves me with blank files and append adds to the end. I tried something like: open(MYFILE,'+',$myfile); and that didn't work. If you are interested here is my code: .. . . open(HTMFILE,'+', $filename) or die Can't Open: $!;# now go through all .htm files and make changes while (HTMFILE) { $_ =~ s/leftmargin=0.*bgcolor=#3c3939//g; $_ =~ s/bgcolor=\#5A5D4E/class=topOfPic/ig; } close(HTMFILE); . . . any thoughts . . ? thanks, tim
RE: Read, Write and Append -- is there more?
My thought is this: that's just the way the filesystem works. You really only have two options: you can create a temporary file or you can store the contents of the file in memory. Then when you have finished changing the file you can write the new file to the old file's place. It sounds at first like a cumbersome way of going about it, but how else could you do it? You would have to make the whole system of file access and writing much more complicated if you allowed people to replace data from the file with data that was larger or smaller than the data being replaced. Do you see what I mean? If the thing you are replacing it with will always be the same length and the thing you are replacing, then you can use sysread or something along those lines, but then you're really doing a whole lot of extra work for no significant gain. -Original Message- From: Tim Booher [mailto:[EMAIL PROTECTED]] Sent: Monday, September 02, 2002 12:09 PM To: [EMAIL PROTECTED] Subject: Read, Write and Append -- is there more? Hello, I am currently frustrated trying to do a simple find and replace on a file, but the three open options: Read, Write and Append don't seem to give me what I want. For example, Read, works on the finding, but nothing will replace. Write erases all the files and leaves me with blank files and append adds to the end. I tried something like: open(MYFILE,'+',$myfile); and that didn't work. If you are interested here is my code: ... . . open(HTMFILE,'+', $filename) or die Can't Open: $!;# now go through all .htm files and make changes while (HTMFILE) { $_ =~ s/leftmargin=0.*bgcolor=#3c3939//g; $_ =~ s/bgcolor=\#5A5D4E/class=topOfPic/ig; } close(HTMFILE); . . . any thoughts . . ? thanks, tim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Read, Write and Append -- is there more?
On Mon, 2 Sep 2002, Tim Booher wrote: Hello, I am currently frustrated trying to do a simple find and replace on a file, but the three open options: Read, Write and Append don't seem to give me what I want. For example, Read, works on the finding, but nothing will replace. Write erases all the files and leaves me with blank files and append adds to the end. I tried something like: open(MYFILE,'+',$myfile); and that didn't work. If you are interested here is my code: .. . . open(HTMFILE,'+', $filename) or die Can't Open: $!;# now go through all .htm files and make changes while (HTMFILE) { $_ =~ s/leftmargin=0.*bgcolor=#3c3939//g; $_ =~ s/bgcolor=\#5A5D4E/class=topOfPic/ig; You are modifying the line that you have read in, but you are not writing it back. You will have to open a temporary file write the modified contents to it and copy it back to original file for a find and replace. There are other ways to this, for e.g. using $^I (perldoc perlvar) Here is an e.g. where I find and replace 'abcd' with 'efgh' in the file 'test.txt'. Actually perl internally renames the original file, opens a new file with the name of the original file to do the processing. In effect it is pretty much the same. my $myfile = 'test.txt'; { local @ARGV = ($myfile); local $^I = '~'; # perldoc perlvar, # perldoc perlrun, read through section explaining the -i switch while () { # perldoc perlsyn s/abcd/efgh/g; print; } } The same can also be done as an oneliner perl -i~ -pe 's/abcd/efgh/g' test.txt You can use any one of these methods for your job. Note: For HTML parsing you might also want to take look at modules that might do this for you. http://search.cpan.org } close(HTMFILE); . . . -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Read, Write and Append -- is there more?
Tim Booher wrote: Hello, I am currently frustrated trying to do a simple find and replace on a file, but the three open options: Read, Write and Append don't seem to give me what I want. For example, Read, works on the finding, but nothing will replace. Write erases all the files and leaves me with blank files and append adds to the end. I tried something like: open(MYFILE,'+',$myfile); and that didn't work. If you are interested here is my code: .. . . open(HTMFILE,'+', $filename) or die Can't Open: $!;# now go through all .htm files and make changes while (HTMFILE) { $_ =~ s/leftmargin=0.*bgcolor=#3c3939//g; $_ =~ s/bgcolor=\#5A5D4E/class=topOfPic/ig; } close(HTMFILE); . . . any thoughts . . ? Probably the simplest way is to use Tie::File. use Tie::File; tie my @data, 'Tie::File', $myfile or die Cannot tie $myfile: $!; for ( @data ) { s/leftmargin=0.*bgcolor=#3c3939//g; s/bgcolor=#5A5D4E/class=topOfPic/ig; } untie @data; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Read, Write and Append -- is there more?
This is the easiest way, in my opinion. It'll work on files passes as command line arguments. Beware the replacing though, files will be modified in place and data may be lost! #!/usr/bin/perl -pi s/leftmargin=0.*bgcolor=#3c3939//g; s/bgcolor=\#5A5D4E/class=topOfPic/ig; On Monday, September 2, 2002, at 02:09 PM, Tim Booher wrote: Hello, I am currently frustrated trying to do a simple find and replace on a file, but the three open options: Read, Write and Append don't seem to give me what I want. For example, Read, works on the finding, but nothing will replace. Write erases all the files and leaves me with blank files and append adds to the end. I tried something like: open(MYFILE,'+',$myfile); and that didn't work. If you are interested here is my code: .. . . open(HTMFILE,'+', $filename) or die Can't Open: $!;# now go through all .htm files and make changes while (HTMFILE) { $_ =~ s/leftmargin=0.*bgcolor=#3c3939//g; $_ =~ s/bgcolor=\#5A5D4E/class=topOfPic/ig; } close(HTMFILE); . . . any thoughts . . ? thanks, tim James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Read, Write and Append -- is there more?
on Tue, 03 Sep 2002 13:11:47 GMT, [EMAIL PROTECTED] (James Edward Gray II) wrote: This is the easiest way, in my opinion. It'll work on files passes as command line arguments. Beware the replacing though, files will be modified in place and data may be lost! #!/usr/bin/perl -pi The loss of data can easily be prevented by using the following shebang instead: #!/usr/bin/perl -pi.orig which leaves a backup copy of your original data. See perldoc perlrun -- felix -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]