Actually it's a good question, and I should have explained myself. When you
do this line on a file, it slurps up the entire contents of the file into an
array:
@infile = <INFILE>;
So the way to read a huge file without loading the entire file into memory
is to use a while loop:
but this changes the way you go about this a little bit. The code becomes:
##############################
open(INFILE,"/etc/passwd");
open(OUTFILE,">/etc/passwd.bak"); #open a temp file to write
while(<INFILE>){ #loads each line into $_, one line per loop
if($_ =~ /^ftp/i){ #if it starts with "ftp"
$_ = ''; #make it a zero-length string
}elsif($_ =~ /guest/i){ #else if it contains "guest"
$_ =~ s/guest/ftp/gi;#replace it with "ftp"
}
print OUTFILE $_; #write the changed code to the temp file
}
open(INFILE,"/etc/passwd.bak"); #now we copy the contents of
open(OUTFILE,">/etc/passwd"); #the temp file to the original
while(<INFILE>){
print OUTFILE $_;
}
close INFILE;
close OUTFILE;
unlink "/etc/passwd.bak"; #remove the temp file
######################
So the one real difference is that instead of creating a copy in memory, we
have to create a temporary file to hold the contents of our changes.
-----Original Message-----
From: Langa Kentane
To: 'Timothy Johnson'
Sent: 5/13/02 10:54 PM
Subject: RE: Delete a line and replace
Hi
Just a quick newbie question:
How would you recommend we do that on a big file?
-----Original Message-----
From: Timothy Johnson [mailto:[EMAIL PROTECTED]]
Sent: 13 May 2002 08:05 PM
To: 'Jorge Goncalvez'; [EMAIL PROTECTED]
Subject: RE: Delete a line and replace
You can try something like this:
open(INFILE,"/etc/passwd");
@infile = <INFILE>; #don't do this if your file gets big
close INFILE;
foreach(@infile){
if($_ =~ /^ftp/i){ #if it starts with "ftp"
$_ = ''; #make it a zero-length string
}elsif($_ =~ /guest/i){ #else if it contains "guest"
$_ =~ s/guest/ftp/gi;#replace it with "ftp"
}
}
open(OUTFILE,">/etc/passwd");
print OUTFILE @infile;
-----Original Message-----
From: Jorge Goncalvez [mailto:[EMAIL PROTECTED]]
Sent: Monday, May 13, 2002 1:41 AM
To: [EMAIL PROTECTED]
Subject: Re:Delete a line and replace
Hi, i have this file /etc/passwd like this:
Everyone:*:0:0:,S-1-1-0::
SYSTEM:*:18:18:,S-1-5-18::
Administrators:*:544:544:,S-1-5-32-544::
Administrator:unused_by_nt/2000/xp:500:513:U-LININF54\Administrator,S-1-
5-21
-175 7981266-2139871995-725345543-500:/home/Administrator:/bin/bash
anonymous:unused_by_nt/2000/xp:1017:513:U-LININF54\anonymous,S-1-5-21-17
5798
1266
-2139871995-725345543-1017:/home/anonymous:/bin/bash
ftp:unused_by_nt/2000/xp:1018:513:U-LININF54\ftp,S-1-5-21-1757981266-213
9871
995-
725345543-1018:/:/bin/bash
Guest:unused_by_nt/2000/xp:501:513:U-LININF54\Guest,S-1-5-21-1757981266-
2139
8719
95-725345543-501:/home/Guest:/bin/bash
HelpAssistant:unused_by_nt/2000/xp:1000:513:Remote Desktop Help
Assistant
Account,U-LININF54\HelpAssistant,S-1-5-21-1757981266-2139871995-72534554
3-10
00:/
home/HelpAssistant:/bin/bash
I wanted to delete all the line which begins with ftp and then replace
Guest
by
ftp in all file.
Thanks.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]