?
Solved

change a file

Posted on 2000-04-15
2
Medium Priority
?
161 Views
Last Modified: 2013-12-25
hello,
in my PERL CGI program, i want to
delete a line from my data.txt
if the line match the input of uers.

i mean:
my data.txt:
apple:45
egg:88
...
...

if a user want to delete apple,
then i need to delete the apple:45 from
my data.txt.

how can i do that?

0
Comment
Question by:mwhuen
[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
2 Comments
 
LVL 1

Accepted Solution

by:
lambda earned 30 total points
ID: 2719282
## $input contains the value to be deleted.

open (FILE_HANDLE, "/path/to/data.txt");
@fileLines = <FILEHANDLE>;
close (FILEHANDLE);

foreach $line (@fileLines)
{
      if ($line =~ /$input/)
      {
            next;
      }
      else
      {
            push (@fileLines1, $line);
      }
}

open (FILE_HANDLE1, ">/path/to/data.txt");
print FILE_HANDLE1 @fileLines1;
close (FILEHANDLE1);

Hope this helps.

£.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2719521
perldoc -q "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?"
0

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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