Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > deleting data from a text file

Reply
Thread Tools

deleting data from a text file

 
 
Shaf
Guest
Posts: n/a
 
      10-11-2004
I am new to Perl and have a question regarding deleting header data
from a text file. I have a set of files that I need to delete data
from. Is there something in Perl to delete string data? What is the
best way for me to handle this?
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-11-2004
"Shaf" <> wrote in message
news: om...
> I am new to Perl and have a question regarding deleting header data
> from a text file. I have a set of files that I need to delete data
> from. Is there something in Perl to delete string data? What is the
> best way for me to handle this?


There are no good ways to simply remove arbitrary text from an existing
file. The general way to do what you want it to open the existing file,
read it, close it, modify the data as needed, re-open the file for
writing (thus eliminating all previous contents), and write the modified
data to the file.

Perl gives a very quick way to do this, via the -n and -i options. You
can read about them by typing
perldoc perlrun
into your command line

In general, you can accomplish your goal like this:
perl -ni.bak -e 'print unless /HEADER/' file.txt

This will remove all lines from file.txt which contain 'HEADER'. It
will save a backup of the original file as file.txt.bak, just in case.

Hope this helps get you started
Paul Lalli


 
Reply With Quote
 
 
 
 
Lars Eighner
Guest
Posts: n/a
 
      10-11-2004
In our last episode,
< >,
the lovely and talented Shaf
broadcast on comp.lang.perl.misc:

> I am new to Perl and have a question regarding deleting header data
> from a text file. I have a set of files that I need to delete data
> from. Is there something in Perl to delete string data?


Lots of things. This is the sort of thing that perl is especially
good for.

> What is the best way for me to handle this?


Not enough information. Are the strings to be deleted unique? Do
they fit a unique pattern? Are the headers you want to delete always
the same length and in the same position in the file? Is there
a unique separator between the header information and the text?
Are there embedded newlines in some of the headers?

In the simplest cases, the s/// operator can be used to search
and replace, searching for the unique header strings or for a
regular expression for the unique header string pattern and
replacing with nothing. This can often be done with a perl
"one-liner."

--
Lars Eighner -finger for geek code- http://www.io.com/~eighner/
If it wasn't for muscle spasms, I wouldn't get any exercise at all.
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      10-11-2004
Shaf wrote:

> I am new to Perl and have a question regarding deleting header data
> from a text file. I have a set of files that I need to delete data
> from. Is there something in Perl to delete string data? What is the
> best way for me to handle this?


read the file
remove the lines you don't want
write it back (maybe make a back up)

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-11-2004
Shaf <> wrote:

> I am new to Perl



You should not overlook the most valuable resource for a Perl
programmer, namely the documentation that ships with the perl distribution.


> and have a question regarding deleting header data

^^^^^
> from a text file.



Your Question is Asked Frequently, you should check the Perl FAQ *before*
posting to the Perl newsgroup:

perldoc -q delete

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?


> What is the
> best way for me to handle this?



The way the FAQ says to.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-12-2004
Shaf wrote:
> I am new to Perl and have a question regarding deleting header data
> from a text file. I have a set of files that I need to delete data
> from. Is there something in Perl to delete string data? What is the
> best way for me to handle this?


This _Q_uestion is _A_sked _F_requently, please check "perldoc -q delete":

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

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muñoz ASP .Net 3 06-04-2005 10:56 AM
Deleting Content of a text file GMK ASP .Net 2 03-05-2005 07:33 AM
deleting data from text files Cancerbero C Programming 4 08-09-2004 07:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57