Read File and Output to new format

Discussion in 'Perl Misc' started by ansher, Mar 24, 2009.

  1. ansher

    ansher Guest

    Hello

    What I am trying to achieve is to read a file and write the formatted
    text into a new file. But I am facing some difficulty with that

    Format: I am reading a file where line continuation is indicated by
    “+” sign at the beginning of the new line. Remove the line
    continuation and add (join) this new line to prev. one

    NOTE: file do not have line number, it is only for your reference.

    Line13: n01contacts/friends do you
    Line14: +have in each of the services ;c=contacts(433)’1’

    Output: n01 contacts/friends do you have in each of the
    services ;c=contacts(433)’1’

    Challenges: I cannot always consider “+”sign as the beginning of the
    new line, those instances are

    1.Section of lines should not be formatted after text ‘fld’, refer
    line 16-23 should not be formatted. Lines after this section must be
    formatted.

    2. Section of lines should not be formatted after text ‘val’, refer
    line 29-33 should not be formatted. Lines after this section must be
    formatted.

    3. Refer line 9-12, line 10 ends with ; but lines 11 & 12 do not.
    After formatting line should be separated by single ;

    *include q10.qin;txt=UNIQUENESS; bb=Top line; tx=Mid Value;tb=Low
    Value

    4.Refer line 13-14,

    Line13: n01contacts/friends do you
    Line14: +have in each of the services ;c=contacts(433)’1’

    After formatting line should not be separated by single ; instead it
    should read as

    n01contacts/friends do you have in each of the services ;c=contacts
    (433)’1’


    Any solution to this will be of great help to me. Thanks
    Format of original file:
    Line1: n010-10 ;c=temp(433)'1'
    Line2: n0111-25 ;c=temp(433)'2'
    Line3: n0126-50 ;c=temp(433)'3'
    Line4: n0151-100 ;c=temp(433)'4'
    Line5: n01101-150 ;c=temp(433)'5'
    Line6: net1151 CONTACTS OR MORE (NET)
    Line7: n01151-200 ;c=temp(433)'6'
    Line8: n01200+ ;c=temp(433)'7'
    Line9: *include q10.qin;txt=UNIQUENESS
    Line10: +bb=Top line;
    Line11: +tx=Mid Value
    Line12: +tb=Low Value
    Line13: n01contacts/friends do you
    Line14: +have in each of the services ;c=contacts(433)’1’
    Line15: net1CONVENIENCE (NET)
    Line16: fld c(m00),c(m96):3
    Line17: +Easy to find/noticeable=044
    Line18: +Quick=045
    Line19: +Convenient=046
    Line20: +Other convenience comments=050;%nosort
    Line21: netend1
    Line22: fld c(m00),c(m96):3
    Line23: +NA=996;%nosort
    Line24: *include lib\9pscale.qin;col(a)=697
    Line25: +txt1=Value 1
    Line26: +txt2=
    Line27: +txt3=
    Line28: +txt4=Value 4
    Line29: val c(650,652);i
    Line30: +1-25=1-25
    Line31: +26-50=26-50
    Line32: +51-75=51-75
    Line33: +76-100=76-100
    Line34: n01Clear testing/QA
    Line35: +requirements ;c=c100'1'

    Expected outputs after formatting:

    n010-10 ;c=temp(433)'1'
    n0111-25 ;c=temp(433)'2'
    n0126-50 ;c=temp(433)'3'
    n0151-100 ;c=temp(433)'4'
    n01101-150 ;c=temp(433)'5'
    net1151 CONTACTS OR MORE (NET)
    n01151-200 ;c=temp(433)'6'
    n01200+ ;c=temp(433)'7'
    *include q10.qin;txt=UNIQUENESS;bb=Top line;tx=Mid Value;tb=Low Value
    n01 contacts/friends do you have in each of the services ;c=contacts
    (433)’1’
    net1CONVENIENCE (NET)
    fld c(m00),c(m96):3
    +Easy to find/noticeable=044
    +Quick=045
    +Convenient=046
    +Other convenience comments=050;
    netend1
    fld c(m00),c(m96):3
    +NA=996;%nosort
    *include lib\9pscale.qin;col(a)=697;txt1=Value
    1;txt2=;txt3=;txt4=Value 4
    val c(650,652);i
    +1-25=1-25
    +26-50=26-50
    +51-75=51-75
    +76-100=76-100
    n01Clear testing/QA
    requirements ;c=c100'1'
     
    ansher, Mar 24, 2009
    #1
    1. Advertising

  2. ansher

    Guest

    On Tue, 24 Mar 2009 06:07:16 -0700 (PDT), ansher <> wrote:

    >Hello
    >
    >What I am trying to achieve is to read a file and write the formatted
    >text into a new file. But I am facing some difficulty with that
    >
    >Format: I am reading a file where line continuation is indicated by
    >“+” sign at the beginning of the new line. Remove the line
    >continuation and add (join) this new line to prev. one
    >
    >NOTE: file do not have line number, it is only for your reference.
    >
    >Line13: n01contacts/friends do you
    >Line14: +have in each of the services ;c=contacts(433)’1’
    >
    >Output: n01 contacts/friends do you have in each of the
    >services ;c=contacts(433)’1’
    >
    >Challenges: I cannot always consider “+”sign as the beginning of the
    >new line, those instances are
    >
    >1.Section of lines should not be formatted after text ‘fld’, refer
    >line 16-23 should not be formatted. Lines after this section must be
    >formatted.
    >
    >2. Section of lines should not be formatted after text ‘val’, refer
    >line 29-33 should not be formatted. Lines after this section must be
    >formatted.
    >
    >3. Refer line 9-12, line 10 ends with ; but lines 11 & 12 do not.
    > After formatting line should be separated by single ;
    >
    > *include q10.qin;txt=UNIQUENESS; bb=Top line; tx=Mid Value;tb=Low
    >Value
    >
    >4.Refer line 13-14,
    >
    >Line13: n01contacts/friends do you
    >Line14: +have in each of the services ;c=contacts(433)’1’
    >
    >After formatting line should not be separated by single ; instead it
    >should read as
    >
    >n01contacts/friends do you have in each of the services ;c=contacts
    >(433)’1’
    >
    >
    >Any solution to this will be of great help to me. Thanks


    But what about ';' ?
    Sure the sample in this form can be done. Not quite so simple
    though. The rules you specify are not quite distinct.

    -sln

    --------------------------------------
    use strict;
    use warnings;

    my ($buf, $skip) = ('', 0);

    while ( <DATA> )
    {
    chomp;
    if ( /^\s* (?: fld | val )/x )
    {
    $skip = 1;
    }
    elsif ( /^\s*\+/ )
    {
    unless ( $skip || !$buf )
    {
    s/;+$//; s/^\s*\++//;
    if ( /^\s*\w*\s*=/ )
    {
    $buf =~ s/;+$//;
    $buf .= ';';
    } else {
    $buf .= ' '
    }
    $buf .= $_;
    $skip = 0;
    next;
    }
    }
    else {
    $skip = 0;
    }

    print $buf,"\n" if ( $buf );
    $buf = $_;
    }

    print $buf,"\n" if ( $buf );


    __DATA__


    n010-10 ;c=temp(433)'1'
    n0111-25 ;c=temp(433)'2'
    n0126-50 ;c=temp(433)'3'
    n0151-100 ;c=temp(433)'4'
    n01101-150 ;c=temp(433)'5'
    net1151 CONTACTS OR MORE (NET)
    n01151-200 ;c=temp(433)'6'
    n01200+ ;c=temp(433)'7'
    *include q10.qin;txt=UNIQUENESS
    +bb=Top line;
    +tx=Mid Value
    +tb=Low Value
    n01contacts/friends do you
    +have in each of the services ;c=contacts(433)’1’
    net1CONVENIENCE (NET)
    fld c(m00),c(m96):3
    +Easy to find/noticeable=044
    +Quick=045
    +Convenient=046
    +Other convenience comments=050;%nosort
    netend1
    fld c(m00),c(m96):3
    +NA=996;%nosort
    *include lib\9pscale.qin;col(a)=697
    +txt1=Value 1
    +txt2=
    +txt3=
    +txt4=Value 4
    val c(650,652);i
    +1-25=1-25
    +26-50=26-50
    +51-75=51-75
    +76-100=76-100
    n01Clear testing/QA
    +requirements ;c=c100'1'
     
    , Mar 24, 2009
    #2
    1. Advertising

  3. ansher <> wrote:

    > Format: I am reading a file where line continuation is indicated by
    > ?+? sign at the beginning of the new line. Remove the line
    > continuation and add (join) this new line to prev. one


    > Challenges: I cannot always consider ?+?sign as the beginning of the
    > new line, those instances are
    >
    > 1.Section of lines should not be formatted after text ?fld?, refer
    > line 16-23 should not be formatted. Lines after this section must be
    > formatted.
    >
    > 2. Section of lines should not be formatted after text ?val?, refer
    > line 29-33 should not be formatted. Lines after this section must be
    > formatted.



    I'll do that part for you.


    > 3. Refer line 9-12, line 10 ends with ; but lines 11 & 12 do not.
    > After formatting line should be separated by single ;
    >
    > *include q10.qin;txt=UNIQUENESS; bb=Top line; tx=Mid Value;tb=Low
    > Value
    >
    > 4.Refer line 13-14,
    >
    > Line13: n01contacts/friends do you
    > Line14: +have in each of the services ;c=contacts(433)?1?
    >
    > After formatting line should not be separated by single ; instead it
    > should read as
    >
    > n01contacts/friends do you have in each of the services ;c=contacts
    > (433)?1?



    I'll leave that so that you have the opportunity to write some code
    yourself, unless you plan to send me your paycheck. :)


    ---------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my $buf='';
    while ( <DATA> ) {
    s/Line\d+: //;
    if ( /^\+/ ) {
    if ( $buf =~ /^(fld|val)/ ) {
    $buf .= $_;
    }
    else {
    s/^\+/ /;
    $buf =~ s/\n/$_/;
    }
    }
    else {
    print $buf;
    $buf = $_;
    }
    }
    print $buf;

    __DATA__
    Line1: n010-10 ;c=temp(433)'1'
    Line2: n0111-25 ;c=temp(433)'2'
    Line3: n0126-50 ;c=temp(433)'3'
    Line4: n0151-100 ;c=temp(433)'4'
    Line5: n01101-150 ;c=temp(433)'5'
    Line6: net1151 CONTACTS OR MORE (NET)
    Line7: n01151-200 ;c=temp(433)'6'
    Line8: n01200+ ;c=temp(433)'7'
    Line9: *include q10.qin;txt=UNIQUENESS
    Line10: +bb=Top line;
    Line11: +tx=Mid Value
    Line12: +tb=Low Value
    Line13: n01contacts/friends do you
    Line14: +have in each of the services ;c=contacts(433)?1?
    Line15: net1CONVENIENCE (NET)
    Line16: fld c(m00),c(m96):3
    Line17: +Easy to find/noticeable=044
    Line18: +Quick=045
    Line19: +Convenient=046
    Line20: +Other convenience comments=050;%nosort
    Line21: netend1
    Line22: fld c(m00),c(m96):3
    Line23: +NA=996;%nosort
    Line24: *include lib\9pscale.qin;col(a)=697
    Line25: +txt1=Value 1
    Line26: +txt2=
    Line27: +txt3=
    Line28: +txt4=Value 4
    Line29: val c(650,652);i
    Line30: +1-25=1-25
    Line31: +26-50=26-50
    Line32: +51-75=51-75
    Line33: +76-100=76-100
    Line34: n01Clear testing/QA
    Line35: +requirements ;c=c100'1'
    ---------------------------


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 24, 2009
    #3
  4. ansher

    ansher Guest

    On Mar 24, 10:18 pm, wrote:
    > On Tue, 24 Mar 2009 06:07:16 -0700 (PDT), ansher <> wrote:
    > >Hello

    >
    > >What I am trying to achieve is to read a file and write the formatted
    > >text into a new file. But I am facing some difficulty with that

    >
    > >Format: I am reading a file where line continuation is indicated by
    > >“+” sign at the beginning of the new line. Remove the line
    > >continuation and add (join) this new line to prev. one

    >
    > >NOTE: file do not have line number, it is only for your reference.

    >
    > >Line13: n01contacts/friends do you
    > >Line14: +have in each of the services  ;c=contacts(433)’1’

    >
    > >Output: n01 contacts/friends do you have in each of the
    > >services  ;c=contacts(433)’1’

    >
    > >Challenges: I cannot always consider “+”sign as the beginning of the
    > >new line, those instances are

    >
    > >1.Section of lines should not be formatted after text ‘fld’, refer
    > >line 16-23 should not be formatted. Lines after this section must be
    > >formatted.

    >
    > >2. Section of lines should not be formatted after text ‘val’, refer
    > >line 29-33 should not be formatted. Lines after this section must be
    > >formatted.

    >
    > >3. Refer line 9-12, line 10 ends with ; but lines 11 & 12 do not.
    > >    After formatting line should be separated by single ;

    >
    > >    *include q10.qin;txt=UNIQUENESS; bb=Top line; tx=Mid Value;tb=Low
    > >Value

    >
    > >4.Refer line 13-14,

    >
    > >Line13: n01contacts/friends do you
    > >Line14: +have in each of the services  ;c=contacts(433)’1’

    >
    > >After formatting line should not be separated by single ; instead it
    > >should read as

    >
    > >n01contacts/friends do you have in each of the services  ;c=contacts
    > >(433)’1’

    >
    > >Any solution to this will be of great help to me. Thanks

    >
    > But what about ';' ?
    > Sure the sample in this form can be done. Not quite so simple
    > though. The rules you specify are not quite distinct.
    >
    > -sln
    >
    > --------------------------------------
    > use strict;
    > use warnings;
    >
    > my ($buf, $skip) = ('', 0);
    >
    > while ( <DATA> )
    > {
    >         chomp;
    >         if ( /^\s* (?: fld | val )/x )
    >         {
    >                 $skip = 1;
    >         }
    >         elsif ( /^\s*\+/ )
    >         {
    >                 unless ( $skip || !$buf )
    >                 {
    >                         s/;+$//; s/^\s*\++//;
    >                         if ( /^\s*\w*\s*=/ )
    >                         {
    >                                 $buf =~s/;+$//;
    >                                 $buf .=';';
    >                         } else {
    >                                  $buf .= ' '
    >                         }
    >                         $buf .= $_;
    >                         $skip = 0;
    >                         next;
    >                 }
    >         }
    >         else {
    >                 $skip = 0;
    >         }
    >
    >         print $buf,"\n" if ( $buf );
    >         $buf = $_;
    >
    > }
    >
    > print $buf,"\n" if ( $buf );
    >
    > __DATA__
    >
    > n010-10        ;c=temp(433)'1'
    > n0111-25       ;c=temp(433)'2'
    > n0126-50       ;c=temp(433)'3'
    > n0151-100      ;c=temp(433)'4'
    > n01101-150     ;c=temp(433)'5'
    > net1151 CONTACTS OR MORE (NET)
    > n01151-200     ;c=temp(433)'6'
    > n01200+        ;c=temp(433)'7'
    > *include q10.qin;txt=UNIQUENESS
    > +bb=Top line;
    > +tx=Mid Value
    > +tb=Low Value
    > n01contacts/friends do you
    > +have in each of the services  ;c=contacts(433)’1’
    > net1CONVENIENCE (NET)
    > fld c(m00),c(m96):3
    > +Easy to find/noticeable=044
    > +Quick=045
    > +Convenient=046
    > +Other convenience comments=050;%nosort
    > netend1
    > fld c(m00),c(m96):3
    > +NA=996;%nosort
    > *include lib\9pscale.qin;col(a)=697
    > +txt1=Value 1
    > +txt2=
    > +txt3=
    > +txt4=Value 4
    > val c(650,652);i
    > +1-25=1-25
    > +26-50=26-50
    > +51-75=51-75
    > +76-100=76-100
    > n01Clear testing/QA
    > +requirements                       ;c=c100'1'- Hide quoted text -
    >
    > - Show quoted text -


    Thank you, Thank you, Thank you, wow - it works like magic. I will
    check this code with similar formatted files tomorrow.

    I am new to perl, didnt understand the logic behind it - too difficult
    for me to understand

    could you please explain it to me? Thanks again :)
     
    ansher, Mar 24, 2009
    #4
  5. ansher

    ansher Guest

    On Mar 25, 1:22 am, Tad J McClellan <> wrote:
    > ansher <> wrote:
    > > Format: I am reading a file where line continuation is indicated by
    > > ?+? sign at the beginning of the new line. Remove the line
    > > continuation and add (join) this new line to prev. one
    > > Challenges: I cannot always consider ?+?sign as the beginning of the
    > > new line, those instances are

    >
    > > 1.Section of lines should not be formatted after text ?fld?, refer
    > > line 16-23 should not be formatted. Lines after this section must be
    > > formatted.

    >
    > > 2. Section of lines should not be formatted after text ?val?, refer
    > > line 29-33 should not be formatted. Lines after this section must be
    > > formatted.

    >
    > I'll do that part for you.
    >
    >
    >
    >
    >
    > > 3. Refer line 9-12, line 10 ends with ; but lines 11 & 12 do not.
    > >     After formatting line should be separated by single ;

    >
    > >     *include q10.qin;txt=UNIQUENESS; bb=Top line; tx=Mid Value;tb=Low
    > > Value

    >
    > > 4.Refer line 13-14,

    >
    > > Line13: n01contacts/friends do you
    > > Line14: +have in each of the services  ;c=contacts(433)?1?

    >
    > > After formatting line should not be separated by single ; instead it
    > > should read as

    >
    > > n01contacts/friends do you have in each of the services  ;c=contacts
    > > (433)?1?

    >
    > I'll leave that so that you have the opportunity to write some code
    > yourself, unless you plan to send me your paycheck.  :)
    >
    > ---------------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my $buf='';
    > while ( <DATA> ) {
    >     s/Line\d+: //;
    >     if ( /^\+/ ) {
    >         if ( $buf =~ /^(fld|val)/ ) {
    >             $buf .= $_;
    >         }
    >         else {
    >             s/^\+/ /;
    >             $buf =~ s/\n/$_/;
    >         }
    >     }
    >     else {
    >         print $buf;
    >         $buf = $_;
    >     }}
    >
    > print $buf;
    >
    > __DATA__
    > Line1: n010-10        ;c=temp(433)'1'
    > Line2: n0111-25       ;c=temp(433)'2'
    > Line3: n0126-50       ;c=temp(433)'3'
    > Line4: n0151-100      ;c=temp(433)'4'
    > Line5: n01101-150     ;c=temp(433)'5'
    > Line6: net1151 CONTACTS OR MORE (NET)
    > Line7: n01151-200     ;c=temp(433)'6'
    > Line8: n01200+        ;c=temp(433)'7'
    > Line9: *include q10.qin;txt=UNIQUENESS
    > Line10: +bb=Top line;
    > Line11: +tx=Mid Value
    > Line12: +tb=Low Value
    > Line13: n01contacts/friends do you
    > Line14: +have in each of the services  ;c=contacts(433)?1?
    > Line15: net1CONVENIENCE (NET)
    > Line16: fld c(m00),c(m96):3
    > Line17: +Easy to find/noticeable=044
    > Line18: +Quick=045
    > Line19: +Convenient=046
    > Line20: +Other convenience comments=050;%nosort
    > Line21: netend1
    > Line22: fld c(m00),c(m96):3
    > Line23: +NA=996;%nosort
    > Line24: *include lib\9pscale.qin;col(a)=697
    > Line25: +txt1=Value 1
    > Line26: +txt2=
    > Line27: +txt3=
    > Line28: +txt4=Value 4
    > Line29: val c(650,652);i
    > Line30: +1-25=1-25
    > Line31: +26-50=26-50
    > Line32: +51-75=51-75
    > Line33: +76-100=76-100
    > Line34: n01Clear testing/QA
    > Line35: +requirements                       ;c=c100'1'
    > ---------------------------
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
    >
    > - Show quoted text -


    This code works but there are few formatting conditions not taken care
    of, like it do not add single ; after joining lines.

    Thanks for your help, I must appreciate the fact you guys (sln & Tad)
    can come up with solution in a flash
     
    ansher, Mar 24, 2009
    #5
  6. ansher <> wrote:
    > On Mar 25, 1:22 am, Tad J McClellan <> wrote:
    >> ansher <> wrote:



    >> > 3. Refer line 9-12, line 10 ends with ; but lines 11 & 12 do not.
    >> >     After formatting line should be separated by single ;

    >>
    >> >     *include q10.qin;txt=UNIQUENESS; bb=Top line; tx=Mid Value;tb=Low
    >> > Value

    >>
    >> > 4.Refer line 13-14,

    >>
    >> > Line13: n01contacts/friends do you
    >> > Line14: +have in each of the services  ;c=contacts(433)?1?

    >>
    >> > After formatting line should not be separated by single ; instead it
    >> > should read as

    >>
    >> > n01contacts/friends do you have in each of the services  ;c=contacts
    >> > (433)?1?

    >>
    >> I'll leave that so that you have the opportunity to write some code
    >> yourself, unless you plan to send me your paycheck.  :)



    > This code works but there are few formatting conditions not taken care
    > of, like it do not add single ; after joining lines.



    Right.

    Did you read what I wrote?

    You must either learn to program in Perl, hire someone to do it
    for you, or abandon getting it done.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 24, 2009
    #6
  7. ansher

    ansher Guest

    > Right.
    >
    > Did you read what I wrote?
    >
    > You must either learn to program in Perl, hire someone to do it
    > for you, or abandon getting it done.
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
    >
    > - Show quoted text -


    Didnt mean any offence, I was just sharing informing with you after
    using the code.

    Thanks again for your help
     
    ansher, Mar 25, 2009
    #7
  8. ansher

    ansher Guest

    On Mar 25, 4:36 am, Tad J McClellan <> wrote:
    > >> I'll leave that so that you have the opportunity to write some code
    > >> yourself, unless you plan to send me your paycheck.  :)

    > > This code works but there are few formatting conditions not taken care
    > > of, like it do not add single ; after joining lines.

    >
    > Right.
    >
    > Did you read what I wrote?
    >
    > You must either learn to program in Perl, hire someone to do it
    > for you, or abandon getting it done.
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -
    >
    > - Show quoted text -


    Didnt mean any offence, I was just sharing information with you after
    using the code.

    Thanks again for your help
     
    ansher, Mar 25, 2009
    #8
    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. chuck amadi
    Replies:
    1
    Views:
    499
    Larry Bates
    Jun 23, 2004
  2. Justme
    Replies:
    9
    Views:
    619
    clayne
    Oct 1, 2006
  3. vinitbhu
    Replies:
    4
    Views:
    409
    Mark Space
    Mar 17, 2008
  4. Randy Kramer
    Replies:
    2
    Views:
    403
    Randy Kramer
    Jan 12, 2007
  5. Alex Dowad
    Replies:
    4
    Views:
    283
    Michel Demazure
    May 1, 2010
Loading...

Share This Page