change a file

Posted on 2000-04-15
Last Modified: 2013-12-25
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:

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

how can i do that?

Question by:mwhuen

Accepted Solution

lambda earned 10 total points
Comment Utility
## $input contains the value to be deleted.

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

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

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

Hope this helps.

LVL 84

Expert Comment

Comment Utility
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?"

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

771 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

11 Experts available now in Live!

Get 1:1 Help Now