?
Solved

Reading a line and then deleting the line

Posted on 2000-05-11
6
Medium Priority
?
171 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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
Suggested Courses

770 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