Read File and Output to new format

A

ansher

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'
 
S

sln

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'
 
T

Tad J McClellan

ansher said:
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'
 
A

ansher

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 :)
 
A

ansher

I'll do that part for you.











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
 
T

Tad J McClellan

ansher said:
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.
 
A

ansher

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
 
A

ansher

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top