formating data

E

eeb4u

I am trying to format data in a file that is bombing at the printer.
The file has the following format:

************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************



************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info



************** 123456 ** C-009
ip address
SERVER 1
some server info



************** 123456 ** F-019
ip address
SERVER 1
some server info

Data Criteria
Each record needs to have exactly 8 lines until the next row of
asterisks (that indicate new record start) Any less rows and the
printer bombs. The first and second records in the above example are
correct.
some records have trailing asterisks, as in first record.
each record has always four or five lines of chars.

Basically the solution needs to insert blank lines if not enough exist.
Unfortunatly, the number of blank lines is constantly changing since
records have above criteria i.e. sometimes 5 lines of text sometimes 4.

Should I strip all blank lines and insert as needed, based on number of
text lines read, or am I going in the wrong direction?

Thanks in advance,

Mike D
 
U

usenet

I am trying to format data in a file that is bombing at the printer.
The file has the following format:
<snip>

But it's unclear what you want to print. Do you want to print the rows
which include the astericks? Both the top and the optional bottom rows
of astericks? ie, do do you want your output to look like this?:

************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************
(blank)
(blank)
(blank)
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info
(blank)
(blank)
(blank)
************** 123456 ** C-009
ip address
SERVER 1
some server info
(blank)
(blank)
(blank)
(blank)
************** 123456 ** F-019
ip address
SERVER 1
some server info
(blank)
(blank)
(blank)
(blank)
 
E

eeb4u

Sorry if it wasn't very clear.

That is exactly what I want to print. I want to print all text that is
in the original file, including all asterisks. The file will need the
extra blank lines to make be sure there are 8 lines (text and blank)
for each record. If the file has 5 lines of char data, it needs 3
blank lines or if file has 4 lines of char data, it needs 4 blanks.
From the small snippet of data I provided, it was supposed to show that
sometimes only 7 lines exist in the record and an additional blank line
would be added in this case.

the original file that I start with has some form feeds that I am
stripping out. Wherever there is a form feed, that particular record
has 7 or 8 lines in it but the the following record always has only 7
lines in it. All other records in file have 8 lines.

Thanks,

Mike D
 
E

eeb4u

(e-mail address removed) kindly replied:
ie, do do you want your output to look like this?:
************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************
(blank)
(blank)
(blank)
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info
(blank)
(blank)
(blank)
************** 123456 ** C-009
ip address
SERVER 1
some server info
(blank)
(blank)
(blank)
(blank)
************** 123456 ** F-019
ip address
SERVER 1
some server info
(blank)
(blank)
(blank)
(blank)


Sorry if it wasn't very clear.


That is exactly what I want to print. I want to print all text that is

in the original file, including all asterisks. The file will need the
extra blank lines to make be sure there are 8 lines (text and blank)
for each record. If the file has 5 lines of char data, it needs 3
blank lines or if file has 4 lines of char data, it needs 4 blanks.

From the small snippet of data I provided, it was supposed to show that

sometimes only 7 lines exist in the record and an additional blank line

would be added in this case.

the original file that I start with has some form feeds that I am
stripping out. Wherever there is a form feed, that particular record
has 7 or 8 lines in it but the the following record always has only 7
lines in it. All other records in file have 8 lines.


Thanks,


Mike D
 
U

usenet

That is exactly what I want to print. I want to print all text that is
in the original file, including all asterisks...

OK, this works. I don't like it because it does two print statements
(one to print the final label). I wouldn't need to do that if I could
check for "eof" of <DATA> so that the conditional would look something
like this:
if ( ($text =~ /^\** / && @label) || eof(*DATA) ) { # ????????
but I don't know how to do that (anyone???)

Anyway, try something like this:

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

my @label;
foreach my $text (<DATA>) {
if ($text =~ /^\** / && @label) {
print $label[$_] || "(blank)\n" for 0..7;
@label = ();
}
push @label, $text;
}
print $label[$_] || "(blank)\n" for 0..7;


__DATA__
************** 123456 ** B-019
ip address
SERVER 1
some server info
******************************

************** 123456 ** B-029
ip address1
ip address2
SERVER 2
some server info

************** 123456 ** C-039
ip address
SERVER 3
some server info

************** 123456 ** F-049
ip address
SERVER 4
some server info
 
U

usenet

Purl said:
For now, but giving benefit of doubt, I consider you to be a troll.

Don't sweat it Mike. If you're lucky, it will killfile you. You could
_ask_ it to killfile you - you might catch it in an agreeable mood. I
think I'm in its killfile.
<PG snipped code - and it does't even matter it didn't use strict!>

I'll admit that the OP's question wasn't entirely clear (per my
follow-up; http://tinyurl.com/dx3ba), but the question was sufficiently
clear that I didn't interpret it as "How do I print seven blank lines
between each line of input?" which is the code that PG offered.
 
A

Anno Siegel

OK, this works. I don't like it because it does two print statements
(one to print the final label). I wouldn't need to do that if I could
check for "eof" of <DATA> so that the conditional would look something
like this:
if ( ($text =~ /^\** / && @label) || eof(*DATA) ) { # ????????
but I don't know how to do that (anyone???)

As the other thread has shown, the problem is the for-loop. With
"while", your conditioal should work as intended.

Anno


[for reference]
my @label;
foreach my $text (<DATA>) {
if ($text =~ /^\** / && @label) {
print $label[$_] || "(blank)\n" for 0..7;
@label = ();
}
push @label, $text;
}
print $label[$_] || "(blank)\n" for 0..7;


__DATA__
************** 123456 ** B-019
ip address
SERVER 1
some server info
******************************

************** 123456 ** B-029
ip address1
ip address2
SERVER 2
some server info

************** 123456 ** C-039
ip address
SERVER 3
some server info

************** 123456 ** F-049
ip address
SERVER 4
some server info
 
U

usenet

Purl said:
You are a bald face liar.

OK, I'll admit I was mistaken about what your code does (I overlooked
$/ = "";). You accuse me of being a liar; I'm not sure what your basis
for that accusation is. I was simply mistaken and did not intend to
deceive.

Your code actually does work..... hey, are those pigs flying past my
window?
 
R

Richard Gration

OK, I'll admit I was mistaken about what your code does (I overlooked
$/ = "";). You accuse me of being a liar; I'm not sure what your basis
for that accusation is. I was simply mistaken and did not intend to
deceive.

Your code actually does work.....

And is faster and more succinct than yours:

[rich@marvin:~/perl]$ ./t.pl 2> /dev/null
Rate Filla Zilla
Filla 5155/s -- -69%
Zilla 16667/s 223% --

[rich@marvin:~/perl]$ cat t.pl
#!/usr/bin/perl
use warnings; use strict;
use Benchmark qw:)all);

cmpthese(10000,{Filla=>\&filla,Zilla=>\&zilla});

sub filla {
my $pos = tell DATA;

my @label;
foreach my $text (<DATA>) {
if ($text =~ /^\** / && @label) {
warn $label[$_] || "(blank)\n" for 0..7;
@label = ();
}
push @label, $text;
}
warn $label[$_] || "(blank)\n" for 0..7;

seek DATA,$pos,0;
}

sub zilla {
my $pos = tell DATA;

$/ = "";

while (<DATA>)
{
do
{ $_ = "$_\n"; }
until ($_ =~ tr/\n/\n/ == 8);
warn $_;
}
seek DATA,$pos,0;
}


__DATA__
************** 123456 ** B-019
ip address
SERVER 1
some server info
******************************

************** 123456 ** B-029
ip address1
ip address2
SERVER 2
some server info

************** 123456 ** C-039
ip address
SERVER 3
some server info

************** 123456 ** F-049
ip address
SERVER 4
some server info



--
"A program, no matter how complex, should act as a single unit. The
program should be directed by the logic within rather than by outward
appearances. If the program fails in these requirements, it will be in a
state of disorder and confusion. The only way to correct this is to
rewrite the program."
-- from The Tao of Programming
 
D

Dr.Ruud

Richard Gration schreef:
do
{ $_ = "$_\n"; }
until ($_ =~ tr/\n/\n/ == 8);

tr works by default on $_, so the '$_ =~ ' part can go.

Another way to bring the \n-count up to 8.

$_ .= "\n" x (8 - tr~\n~\n~);

which is not equivalent to the do-loop, because that has problems when
the count was already 8 or more.
 
E

Eric J. Roode

You have written, paraphrased,

"My way is the only right way."

....which is something that you have written hundreds of times in this
newsgroup.


--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
R

robic0

I am trying to format data in a file that is bombing at the printer.
The file has the following format:

************** 123456 ** B-009
ip address
SERVER 1
some server info [-snip-]
Data Criteria
Each record needs to have exactly 8 lines until the next row of
asterisks (that indicate new record start) Any less rows and the
printer bombs. The first and second records in the above example are
correct.
some records have trailing asterisks, as in first record.
each record has always four or five lines of chars.

Why do you want to send a "line count" oriented record
to the printer? Printers don't "bomb".
Sounds line you are monitoring an ascii printer port stream
to roll records into a database somewhere. Old hospitals used
to do this by running a real-time mainframe (or as400) print job that
prints this info (where the data can't be querried from incompatible
systems). I think I did this at some point in my past.

The first problem is that you rely on the end-of-record
to be a series of blank lines without having a strategy for the
beginning-of-record. The replies here key on that fact
that you emphisize. This is a "no-go" reliability factor.
If your pickup routine on the other end "keys" on that, then its
bad also.
You can't rely on the "end" to be the start of a new "begin".
You can only rely on the new "begin" and thats all. Otherwise,
a fals end could trigger a false-positive beiginning!
You can only rely on a "begin" trigger. Remember you are
writing this to pre-format that which is going to the printer.
Since your post printer program keys on line no's, you can't
get this middle step wrong. I assume you have no control over
the generation of the data file you are reading in.

You will have to qualify this block:

************** 123456 ** C-009
ip address
SERVER 1
some server info

You must key in on something in this line:
"************** 123456 ** C-009"
as the beginning of record. Since you assume
any contrast on this line has more of a chance
to be the beggining of record than any others.
And you have to validate anything else in the
record too (like ip) that its in the correct
spot and within range of what you would expect.
You should also have the logic to "reject" it
as a valid record if its outside the parameters
and not pass it on to the hard size/values/position
the printer expects.

Consider this as a controlled crash, where the
steering by the driver is "estimated" and without
guarante, to maneuver obsticals.

I'm going to quote Perly Girl's code here.
Not to critisize, but because she actually
DID exactly what you requested. No more and
no less. She was 100% right on. I don't think
it should be done this way (just read above).
This scenario is way too problematic and requires
a few more levels of error checking if it were
to be professional code. Code that approaches
Perl 1-liners is very disturbing. Its inherrently
dangerous because of its limited scope (and
caveats).

Streaming data has to be pulled off like train
"boxcars" when it passes into, and lines up
with the correct triggers. It must be qualified
by having the correct front/middle/rear and be
rejectable if not. Everybodys got thier own
level of patients. It just a matter of how much
agravation is acceptable. Dropped records
or mis-aligned boundries (corruption) may
be ok for what this is. Speed though is
NOT and never will be any kind of replacment
for accuracy and probablities of outcome.

- - - - - - - - - - - - - - - - - - - - - -

Remember is just an example (by Purl Gurl),
typically "how" things can go wrong (she
did exactly what you asked for and did it good!).
Sometimes you have to give a little more thought
thou when writing the specs --

--------------
Purl Gurl

$/ = "";
don't recommend this
while (<DATA>)
{
do
{ $_ = "$_\n"; }
until ($_ =~ tr/\n/\n/ == 8); <--- hangs, see below until ($_ =~ tr/\n/\n/ >= 8);
print $_;
}
------------
__DATA__
************** 123456 ** B-009
ip address
SERVER 1
some server info


******************************
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info


Prints this:
-------------------------
************** 123456 ** B-009
ip address
SERVER 1
some server info




******************************
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info

__DATA__
************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************
" " <-----
" " <-----

************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info

Hangs the machine !!
 
R

robic0

Remember is just an example (by Purl Gurl),
typically "how" things can go wrong (she
did exactly what you asked for and did it good!).
Sometimes you have to give a little more thought
thou when writing the specs --

--------------

this expects 2 or more blank line terminations
is why I don't recommend this.
" \n"
" \n"
and can hang un-knowing code.
 
R

robic0

As soon as you go over 7 newlines in the data (you add 1
pluss the eor has one)
without having a end of record $/ = "", (blank line),
the until ($_ =~ tr/\n/\n/ == 8) the tr/// will already be 9
and put it into an endless loop adding \n to $_ and eventually
crashing the os. So thats why I recommended a ">= 8" for
safety. Also, when using a blank (\n\n) eor, a line with
just spaces is not counted as a eor. Good to be a little
bullet proof.


$/ = "";

while (<DATA>)
{
my $dd = 0;
do {
$_ = "$_\n";
$dd = $_ =~ tr/\n/\n/;
print $dd,"\n";
} until ($dd >= 10);
print $_;
}


__DATA__
"************** 123456 ** B-009"
"ip address"
"SERVER 1"
"some server info"
"******************************"
" "
" "

----------------------
output:

9
10
"************** 123456 ** B-009"
"ip address"
"SERVER 1"
"some server info"
"******************************"
" "
" "
 
E

Eric J. Roode

Is yours a fact or a childish emotional outburst of a known troll?

No, I'm an unknown troll.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
M

Mike Dundas

I am trying to format data in a file that is bombing at the printer.
The file has the following format:

************** 123456 ** B-009
ip address
SERVER 1
some server info [-snip-]
Data Criteria
Each record needs to have exactly 8 lines until the next row of
asterisks (that indicate new record start) Any less rows and the
printer bombs. The first and second records in the above example are
correct.
some records have trailing asterisks, as in first record.
each record has always four or five lines of chars.

Why do you want to send a "line count" oriented record
to the printer? Printers don't "bomb".
Sounds line you are monitoring an ascii printer port stream
to roll records into a database somewhere. Old hospitals used
to do this by running a real-time mainframe (or as400) print job that
prints this info (where the data can't be querried from incompatible
systems). I think I did this at some point in my past.

The first problem is that you rely on the end-of-record
to be a series of blank lines without having a strategy for the
beginning-of-record. The replies here key on that fact
that you emphisize. This is a "no-go" reliability factor.
If your pickup routine on the other end "keys" on that, then its
bad also.
You can't rely on the "end" to be the start of a new "begin".
You can only rely on the new "begin" and thats all. Otherwise,
a fals end could trigger a false-positive beiginning!
You can only rely on a "begin" trigger. Remember you are
writing this to pre-format that which is going to the printer.
Since your post printer program keys on line no's, you can't
get this middle step wrong. I assume you have no control over
the generation of the data file you are reading in.

You will have to qualify this block:

************** 123456 ** C-009
ip address
SERVER 1
some server info

You must key in on something in this line:
"************** 123456 ** C-009"
as the beginning of record. Since you assume
any contrast on this line has more of a chance
to be the beggining of record than any others.
And you have to validate anything else in the
record too (like ip) that its in the correct
spot and within range of what you would expect.
You should also have the logic to "reject" it
as a valid record if its outside the parameters
and not pass it on to the hard size/values/position
the printer expects.

Consider this as a controlled crash, where the
steering by the driver is "estimated" and without
guarante, to maneuver obsticals.

I'm going to quote Perly Girl's code here.
Not to critisize, but because she actually
DID exactly what you requested. No more and
no less. She was 100% right on. I don't think
it should be done this way (just read above).
This scenario is way too problematic and requires
a few more levels of error checking if it were
to be professional code. Code that approaches
Perl 1-liners is very disturbing. Its inherrently
dangerous because of its limited scope (and
caveats).

Streaming data has to be pulled off like train
"boxcars" when it passes into, and lines up
with the correct triggers. It must be qualified
by having the correct front/middle/rear and be
rejectable if not. Everybodys got thier own
level of patients. It just a matter of how much
agravation is acceptable. Dropped records
or mis-aligned boundries (corruption) may
be ok for what this is. Speed though is
NOT and never will be any kind of replacment
for accuracy and probablities of outcome.

- - - - - - - - - - - - - - - - - - - - - -

Remember is just an example (by Purl Gurl),
typically "how" things can go wrong (she
did exactly what you asked for and did it good!).
Sometimes you have to give a little more thought
thou when writing the specs --

--------------
Purl Gurl

$/ = "";
don't recommend this
while (<DATA>)
{
do
{ $_ = "$_\n"; }
until ($_ =~ tr/\n/\n/ == 8); <--- hangs, see below until ($_ =~ tr/\n/\n/ >= 8);
print $_;
}
------------
__DATA__
************** 123456 ** B-009
ip address
SERVER 1
some server info


******************************
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info


Prints this:
-------------------------
************** 123456 ** B-009
ip address
SERVER 1
some server info




******************************
************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info

__DATA__
************** 123456 ** B-009
ip address
SERVER 1
some server info
******************************
" " <-----
" " <-----

************** 123456 ** B-009
ip address1
ip address2
SERVER 1
some server info

Hangs the machine !!

I use the term *bomb* to mean printing fails. I should have been more
specific and stated that the labels print incorrectly. I have the script
working now after using the suggestions from the original responder. Thanks
to David Filmer all others who replied with serious suggestions.


Mike
 

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

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top