deleting data from a text file

Discussion in 'Perl Misc' started by Shaf, Oct 11, 2004.

  1. Shaf

    Shaf Guest

    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?
    Shaf, Oct 11, 2004
    #1
    1. Advertising

  2. Shaf

    Paul Lalli Guest

    "Shaf" <> wrote in message
    news:...
    > 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
    Paul Lalli, Oct 11, 2004
    #2
    1. Advertising

  3. Shaf

    Lars Eighner Guest

    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.
    Lars Eighner, Oct 11, 2004
    #3
  4. Shaf

    John Bokma Guest

    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
    John Bokma, Oct 11, 2004
    #4
  5. 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
    Tad McClellan, Oct 11, 2004
    #5
  6. 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
    Jürgen Exner, Oct 12, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GMK

    Deleting Content of a text file

    GMK, Mar 4, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    381
  2. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,403
    Scott Allen
    Jun 4, 2005
  3. Cancerbero

    deleting data from text files

    Cancerbero, Aug 7, 2004, in forum: C Programming
    Replies:
    4
    Views:
    298
    Cedric LEMAIRE
    Aug 9, 2004
  4. Harry Barker
    Replies:
    2
    Views:
    512
    Alf P. Steinbach
    Apr 19, 2006
  5. crea
    Replies:
    2
    Views:
    406
    Nobody
    Dec 28, 2012
Loading...

Share This Page