Solved

Reading a line and then deleting the line

Posted on 2000-05-11
6
169 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 5 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

728 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