Solved

change a file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now