Delete lines in a file

Discussion in 'Perl Misc' started by Adam, Dec 1, 2004.

  1. Adam

    Adam Guest

    Hello:

    I am trying to solve the following problem:

    A file, on each line contains typically 5 columns (separated by tabs)
    of numbers. Ocassionally, on a line, there appears a single number in
    the first column. I want to delete occurrences of such lines (i.e.
    lines that only contain a single number.

    For example

    1.2345 2.3456 3.4567 4.5678 5.6789 ---> Typical line
    23 ---> Atypical line (delete this
    occurence)

    How can I do this in Perl ?

    Thanks
     
    Adam, Dec 1, 2004
    #1
    1. Advertisements

  2. Adam wrote:
    > A file, on each line contains typically 5 columns (separated by tabs)
    > of numbers. Ocassionally, on a line, there appears a single number in
    > the first column. I want to delete occurrences of such lines (i.e.
    > lines that only contain a single number.


    Your Question is Asked Frequently, see
    perldoc -q "delete a line"

    jue
     
    Jürgen Exner, Dec 1, 2004
    #2
    1. Advertisements

  3. Adam

    Paul Lalli Guest

    "Jürgen Exner" <> wrote in message
    news:pYlrd.10496$xk5.10301@trnddc09...
    > Adam wrote:
    > > A file, on each line contains typically 5 columns (separated by

    tabs)
    > > of numbers. Ocassionally, on a line, there appears a single number

    in
    > > the first column. I want to delete occurrences of such lines (i.e.
    > > lines that only contain a single number.

    >
    > Your Question is Asked Frequently, see
    > perldoc -q "delete a line"


    I disagree that Tie::File is needed for this operation. This would
    suffice, no?

    perl -ni.bak -e'print unless /^\d+$/' file.txt

    (obviously, may need to modify the regexp if decimals or other
    representations of numbers are allowed on the atypical lines)

    Paul Lalli
     
    Paul Lalli, Dec 1, 2004
    #3
  4. Adam

    J. Romano Guest

    (Adam) wrote in message news:<>...
    >
    > A file, on each line contains typically 5 columns (separated by tabs)
    > of numbers. Ocassionally, on a line, there appears a single number in
    > the first column. I want to delete occurrences of such lines (i.e.
    > lines that only contain a single number.
    >
    > For example
    >
    > 1.2345 2.3456 3.4567 4.5678 5.6789 ---> Typical line
    > 23 ---> Atypical line (delete this
    > occurence)



    Try this:

    In UNIX:
    perl -ne 'print unless m/^\d+$/' input.txt > output.txt

    In DOS (Win32):
    perl -ne "print unless m/^\d+$/" input.txt > output.txt

    The file "input.txt" will be the file you already have, and
    "output.txt" will be the file that will be created, which will be
    exactly like "input.txt" except that it won't have any lines
    consisting of a single number (i.e., the atypical lines).

    I hope this helps.

    -- Jean-Luc
     
    J. Romano, Dec 1, 2004
    #4
    1. Advertisements

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. Srinivasa T.N.
    Replies:
    7
    Views:
    718
    Amy G
    Dec 1, 2003
  2. Replies:
    4
    Views:
    6,862
  3. Paul LAURENT

    How do I delete lines from a file using "ofstream" ?

    Paul LAURENT, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    691
    CBFalconer
    Nov 7, 2003
  4. Murali
    Replies:
    2
    Views:
    778
    Jerry Coffin
    Mar 9, 2006
  5. Steve Holden
    Replies:
    1
    Views:
    526
    Chris
    Feb 19, 2009
  6. joeyej
    Replies:
    5
    Views:
    278
    Tad McClellan
    Apr 12, 2006
  7. Faith Greenwood

    open file, delete 4 lines if they meet criteria

    Faith Greenwood, Aug 1, 2009, in forum: Perl Misc
    Replies:
    1
    Views:
    208
    Faith Greenwood
    Aug 1, 2009
  8. PerlFAQ Server
    Replies:
    0
    Views:
    249
    PerlFAQ Server
    Jan 31, 2011
Loading...