CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
deleting a line of code

 



QuestOFions
Deleted

May 17, 2000, 5:35 PM

Post #1 of 3 (563 views)
deleting a line of code Can't Post

I have a data file and I wanted to delete a line. How would I go about this? Thank You


Jasmine
Administrator

May 19, 2000, 4:34 AM

Post #2 of 3 (563 views)
Re: deleting a line of code [In reply to] Can't Post

It would be easier to help if you posted just a hint more information Smile

So I'm going to try to help, but will make the following assumptions: you have a pipe-delimited database; you want to delete the line that has "delete me" as the first field.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my $line;
open (DATA,"<whatever.txt") or die "Couldn't open whatever.txt $!\n";
while (<DATA> ){
chomp;
my @list = split(/\|/,$_);
if ($list[0] ne "delete me"){
$line .= $_ . "\n";
}
}
close DATA;

open (WRITE,">whatever.txt") or die "Couldn't open whatever.txt $!\n";
print WRITE $line;
close WRITE
</pre><HR></BLOCKQUOTE>

If this doesn't help, please post your existing code and let us know how you're determining which line to delete.

[This message has been edited by Jasmine (edited 05-19-2000).]


TheGame+
Deleted

May 19, 2000, 6:23 AM

Post #3 of 3 (563 views)
Re: deleting a line of code [In reply to] Can't Post

You could also use the following command-line :

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

perl -pi.bak -e "s/^delete me.*\n//" yourfile</pre><HR></BLOCKQUOTE>

which basically replaces the whol e'delete me ...' line with nothing.

You can find more information about this in the Perl FAQ 5, under the topic
"How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?"

See http://www.perlguru.com/perldocs/pod/perlfaq5.html

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives