RE: reading/writing to a ascii file
ok let's say I have a 500,000 line file... What I want is for the script to read the file in line by line and only change the 1 line I am looking for. ex: line 550 contains the line I am looking for I want to change that line and not have to re-create the file. You could do this: perl -i -p -e s/search/replace/ig $filename Good luck! -Erich- ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: reading/writing to a ascii file
Ryan, it's easier than you think: open(IN, $inputfile) or die can't open $inputfile: $!; open(OUT,$outputfile) or die can't open $outputfile: $!; while (IN){ s/$string/$replacement/g; print OUT $_; } close IN; close OUT; rename($outputfile, $inputfile) or die can't rename... $!; -- Mark Thomas[EMAIL PROTECTED] Internet Systems Architect User Technology Associates, Inc. $_=q;KvtuyboopuifeyQQfeemyibdlfee;; y.e.s. ;y+B-x+A-w+s; ;y;y; ;;print;; ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: reading/writing to a ascii file
Title: RE: reading/writing to a ascii file That is still opening and reading the entire file and then creating an entire new file... I may run into instances where the file will be opened and modified many times. -Original Message- From: Thomas, Mark - BLS CTR [mailto:[EMAIL PROTECTED]] Sent: Tuesday, October 28, 2003 9:53 AM To: 'Farrington, Ryan'; [EMAIL PROTECTED] Subject: RE: reading/writing to a ascii file Ryan, it's easier than you think: open(IN, $inputfile) or die can't open $inputfile: $!; open(OUT,$outputfile) or die can't open $outputfile: $!; while (IN){ s/$string/$replacement/g; print OUT $_; } close IN; close OUT; rename($outputfile, $inputfile) or die can't rename... $!; -- Mark Thomas [EMAIL PROTECTED] Internet Systems Architect User Technology Associates, Inc. $_=q;KvtuyboopuifeyQQfeemyibdlfee;; y.e.s. ;y+B-x+A-w+s; ;y;y; ;;print;;
RE: reading/writing to a ascii file
That is still opening and reading the entire file and then creating an entire new file... No, it's not. It reads only one line at a time into memory. This is quite different from your version which read the entire file into memory. Are you looking to avoid *both* reading the file into memory and creating a temp file? If so, look into the Tie::File module. I believe you'd have to lock the file to prevent other processes from reading the file while it is tied. -- Mark Thomas[EMAIL PROTECTED] Internet Systems Architect User Technology Associates, Inc. $_=q;KvtuyboopuifeyQQfeemyibdlfee;; y.e.s. ;y+B-x+A-w+s; ;y;y; ;;print;; ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: reading/writing to a ascii file
Ok guys here is the pickle I'm in now =-) I need to read from a ASCII file and only modify the lines that match a variable. I do not want to read the entire file into memory and write the entire file out each time I need to make a change. Here is what I am doing that reads the entire file and re-writes the entire file: $var1 = '106253498'; open(FILE, my.file); @file = FILE; close(FILE); open(FILE, my.file); chomp @file; foreach my $line (@file){ if ($line =~ /^$var1\|/){ #do my data changes $line = asdf1234;#where asdf1234 is my changes } print FILE $line . \n; } close(FILE); Can anyone offer a suggestion? My issue is that the data that is coming in is x length (where x is a dynamic space) and the value I am writing back to the line can be a max of x+10 and a min of x-10.. You could use Tie::File... use Tie::File; $var1 = '106253498'; tie my @hash, 'Tie::File', my.file; foreach (@hash) { if (/^$var1\|/) { $_ = asdf1234; } } untie @hash; ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs