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. Advertising

  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. Advertising

  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. 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. Srinivasa T.N.
    Replies:
    7
    Views:
    564
    Amy G
    Dec 1, 2003
  2. Replies:
    4
    Views:
    6,692
  3. Joe Wright
    Replies:
    0
    Views:
    554
    Joe Wright
    Jul 27, 2003
  4. Paul LAURENT

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

    Paul LAURENT, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    624
    CBFalconer
    Nov 7, 2003
  5. Murali
    Replies:
    2
    Views:
    610
    Jerry Coffin
    Mar 9, 2006
Loading...

Share This Page