Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading a line and then deleting the line

Posted on 2000-05-11
6
Medium Priority
?
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 15 total points
ID: 2801599
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
ID: 2803764
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
ID: 2804473
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:maneshr
ID: 2805009
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
ID: 2805238
it was cool naneshr... thanks.,.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2805820
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

610 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