• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

change a file

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
mwhuen
Asked:
mwhuen
1 Solution
 
lambdaCommented:
## $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
 
ozoCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now