Deleting newlines in a text file

Discussion in 'Perl Misc' started by toomanyjoes@mail.utexas.edu, Jan 12, 2005.

  1. Guest

    Hello,

    I'm brand new to perl. I need to delete newlines from a text file but
    only in the case that the line does not begin with a number. Then write
    the results into a new text file.

    For instance my text file looks like this.

    1sampletextsampletextsamplet22tsampletextsampletextsampletext
    2sampletextsampletextsampletextsam56etextsampletextsampletext
    3sampletextsampletextsampletextsampletextsampletextsampletext
    8sampletextsampletextsampletex765mpletextsampletextsampletext
    9sampletextsampletextsampletextsampletextsampletextsampletext
    15sampletextsampletextsampletextsampletextsampletextsampletext
    sampletextsampletextsampletextsample324tsampletextsampletext
    18sampletextsampletextsampletextsampletextsampletextsampletext
    sampletextsampletextsampletextsampletextsampletextsampletext
    sampletextsampletext566pletextsampletextsampletextsampletext
    22sampletextsampletextsampletextsampletextsampletextsampletext
    1sampletextsampletextsamplete467ampletextsampletextsampletext

    As you can see I would like every line to START with a number.


    Also after this is done I need another script that will grab the number
    at the beginning and put it in a database field and then grab all the
    "sampletext" up until the next number and put that in another database
    field. This taking into account that there could be numbers within the
    sampletext that are not found at the beginning of a line. These numbers
    should be taken as part of the sample text and not confused with the
    numbers at the beginning of the line.(A little harder I'm sure) Any
    questions you guys can help me with I'll appreciate.


    Thanks!

    Joe
     
    , Jan 12, 2005
    #1
    1. Advertising

  2. wrote:
    > I'm brand new to perl. I need to delete newlines from a text file but
    > only in the case that the line does not begin with a number.
    > [...]


    Just chomp() the line iff it starts with a digit

    chomp if (/^\d/);

    jue
     
    Jürgen Exner, Jan 12, 2005
    #2
    1. Advertising

  3. Jürgen Exner wrote:

    > wrote:
    >> I'm brand new to perl. I need to delete newlines from a text file but
    >> only in the case that the line does not begin with a number.
    >> [...]

    >
    > Just chomp() the line iff it starts with a digit
    >
    > chomp if (/^\d/);
    >

    No, that doesn't do what he wants. What he wants to do is
    chomp iff the *next* line does *not* begin with a digit.

    my $last_line = <>;
    while (<>) {
    chomp ($last_line) if (/^\D/);
    print $last_line;
    $last_line = $_;
    }
    print $last_line;

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Jan 12, 2005
    #3
  4. Jay Tilton Guest

    wrote:

    : I'm brand new to perl. I need to delete newlines from a text file but
    : only in the case that the line does not begin with a number.

    [snip]

    : As you can see I would like every line to START with a number.

    "...only in the case that the _next_ line does not begin with a number"
    seems a more accurate description.

    perl -pe "chomp;print $/ if $.>1 and /^\d/" file1 > file2

    (Alter quotes to suit your shell, naturally.)
     
    Jay Tilton, Jan 12, 2005
    #4
  5. <> wrote:

    > Any
    > questions you guys can help me with I'll appreciate.



    I notice that there *were no* questions in your article.
    (questions end with a question mark.)

    Did you mean to ask some questions?

    Show us what you have tried so far, and we will help you fix it.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 12, 2005
    #5
    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. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    DB Text with NewLines, etc. for display in a Repeater or DataGrid

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Jun 14, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    471
  2. rihad

    newlines in text files

    rihad, Oct 4, 2003, in forum: C Programming
    Replies:
    7
    Views:
    330
    Micah Cowan
    Oct 6, 2003
  3. Thomas Philips

    Reading newlines from a text file

    Thomas Philips, Feb 28, 2004, in forum: Python
    Replies:
    5
    Views:
    414
    Peter Otten
    Feb 29, 2004
  4. Harry Barker
    Replies:
    2
    Views:
    537
    Alf P. Steinbach
    Apr 19, 2006
  5. crea
    Replies:
    2
    Views:
    428
    Nobody
    Dec 28, 2012
Loading...

Share This Page