Solved

change a file

Posted on 2000-04-15
2
157 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 10 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

707 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