Solved

Reading a line and then deleting the line

Posted on 2000-05-11
6
162 Views
Last Modified: 2010-05-18
How do i READ A LINE from a file and then after reading it, then delete that line....  assuming I have a bunch of lines...

thanks
0
Comment
Question by:CODER
  • 4
6 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 5 total points
Comment Utility
here is a script that will do the above for you.

basically this script reads a file and picks all the lines that start with a # (ignoring/deleting the other lines) and write those lines back to the same file.

That in effect simulates a delete of specific lines.

you can replace the if with any condition that you are looking for.

======================coder.pl
#!/usr/local/bin/perl

$file="/tmp/file.txt";
open(FILE,"$file") || die $!;

while(<FILE>){
  if (/^#/){ ## The line starts with a #
    push(@arr,$_); ## Store that line in an array
  }
}
close(FILE);

open(FILE,">$file") || die $!;
foreach(@arr){
  print FILE $_;
}
close(FILE);
0
 
LVL 1

Expert Comment

by:lambda
Comment Utility
if (/^#/){

maneshr: I think this line has to be modified. CODER wants some particular lines to be deleted, so it shld be quite the opposite check. Something like, lines starting with '#' are to be deleted.
 
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
lambda,

like i said in my comment ...

"you can replace the if with any condition that you are looking for. "

the script is just a sample that shows one how to do the "delete".
i have left the exact condition for the delete to the user.

i would suggest that we leave the interpretation of the delete to CODER.

if CODER can provide the exact condition, only then can i modify the if statement.

Rgds
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:maneshr
Comment Utility
CODER,

was the solution that i gave you not up to your requirements??

what additional items were you looking for in your script??

do let me know.
0
 

Author Comment

by:CODER
Comment Utility
it was cool naneshr... thanks.,.
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
glad to know that.

the reason i asked the same was because you had graded the answer B.
therfore i was wondering if i had missed out on some point.

Rgds
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now