deleting data from a text file

S

Shaf

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

Paul Lalli

Shaf said:
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
 
L

Lars Eighner

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."
 
J

John Bokma

Shaf said:
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)
 
T

Tad McClellan

Shaf said:
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.
 
J

Jürgen Exner

Shaf said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top