need help with text wrap problem...

J

jac

Ok, so far so good goes my first *productive* PERL program ever...

This program opens a text file and prints everything except the lines
containing words "adding", "unable", and "PNL". So far it mostly works
like I want it to except that the lines of text that contain the word
"unable" wrap at column "58" to a new line. I need to omit the new line

as well as its part of the line that contains the word I don't want.
Is there a pattern-match that I can use to delete a next line?


---INPUT TEXT -----------------------------------------------------|


196315-2 CASTING,VOLUTE,8.5
2-76811 WILL-WA PTAC02 XT70
Unable to fetch part description for part 210-9--4-04120-
210-9--4-04120-
Unable to fetch part description for part 210-9--4-04120-
210-9--4-04120-
231146-76811 S.S. TAG-REF DESIG LABEL KIT
231157-76811 S.S. TAG INSTALLATION KIT


sample output -------------------------------------------------------|


185650-76811-1 LABEL GATHERING KIT, DRVN, CENT
196315-2 CASTING,VOLUTE,8.5
2-76811 WILL-WA PTAC02 XT70
210-9--4-04120-
210-9--4-04120-
231146-76811 S.S. TAG-REF DESIG LABEL KIT
231157-76811 S.S. TAG INSTALLATION KIT


---------------------------------------------------------------------------­--|



In the sample output, the numbers "210-9--4-04120-" are wrapped from a
line above it that contained the word "unable"


--- code example-----------------------------------------|


open PULLIN,"$in" || die "$in does not exist: $!\n";
open PULLOUT, ">output.txt" || die "$!\n";


while (<PULLIN>) {
if (! /adding|unable|PNL/i) {
print PULLOUT;
}


}


close(PULLIN);
close(PULLOUT);

--- end code -------------------------------------------------|


Thanks,


Alex
 
J

Jürgen Exner

jac said:
Ok, so far so good goes my first *productive* PERL program ever...

This program opens a text file and prints everything except the lines
containing words "adding", "unable", and "PNL". So far it mostly works
like I want it to except that the lines of text that contain the word
"unable" wrap at column "58" to a new line. I need to omit the new
line

So you want to skip the line that is after the line containing 'unable'?
Just read it without processing it further.

[...]
while (<PULLIN>) {
if (! /adding|unable|PNL/i) {
print PULLOUT;

if (/Unable/) {undef = said:

jue
 
J

jac

ok -- good... See? this is part about processing files that confuses
me. I'm still under the impression that PERL or any other language for
that matter, treats a text file as a "glob" -- I don't know a better
way to explain it.

Does PERL process each line individually? and then considers the next
line to be something new?

the file--------------------------------------------------------
one line (do something to this line)
two lines (don't do anything to this line)
three lines (do something else to this line)
 
J

Jürgen Exner

jac said:
ok -- good... See? this is part about processing files that confuses

_WHAT_ is part about ....? Please quote appropriate context -as has been
customary for decades- when replying such that people know what your are
talking about.
me. I'm still under the impression that PERL or any other language for
that matter, treats a text file as a "glob" -- I don't know a better
way to explain it.

It does. Sometimes. Depending on _how_ you are reading the file.
Does PERL process each line individually? and then considers the next
line to be something new?

If you read the file line by line as in
while (<FILE>) {....}
or
$line = <FILE>;
then yes. Also in
@all_lines = <FILE>;
where each array element will contain one line of the file.

If you read the file by other means e.g. using read() then no.
I guess I don't understand what makes a perl program treat each line
individually.

It's the INPUT_RECORD_SEPARATOR variable "$/". Further details see perldoc
perlvar.

jue
 
J

jac

jue,
Thank you for the explanation. Unfortunately, the example you offered
me didn't work for my application.
"if (/Unable/) {undef = <PULLIN>} #read another line and discard it"

How about this?
Is there a way to delete a line of text where the line wraps into the
next line? Here's what I'm thinking about...

-----
thislinewrapsthislinewrapsthislinewrapsthislinewrapsthislinewraps
thislinewrapsthislinewraps
----

maybe something line "if charlen > 80; delete the next line;
Does a _line wrap_ character exist in PERL? maybe I can look for that
and delete it...

I just dont' know what PERL syntax to use for this as I've only been
studying PERL for a month and a half. I'm a web designer so I'm not
even thinking like a software engineer. Maybe I need to -- I don't
know.. thanks for your help so far...

Alex
 
C

cmic

Hello Jac,
jac wrote :
jue,
Thank you for the explanation. Unfortunately, the example you offered
me didn't work for my application.


How about this?
Is there a way to delete a line of text where the line wraps into the
next line? Here's what I'm thinking about...

If the line containing "Unable ..." *always* wraps, then you get the
solution : delete every
line after the line containing the "Unable" thing.

But How come your imput file is wrapped ? Can you control the wrapping
part of the process that yields you input file ? I think you'd better
try this first.
Regards
 
J

jac

If the line containing "Unable ..." *always* wraps, then you get the solution : delete every line after the line > containing the "Unable" thing.

agreed, but what does the syntax look like delete a line *after*?
that's what I'm having a difficult time with.
 
X

Xicheng Jia

jac said:
jue,
Thank you for the explanation. Unfortunately, the example you offered
me didn't work for my application.


How about this?
Is there a way to delete a line of text where the line wraps into the
next line? Here's what I'm thinking about...

If you knew the exact position your lines are wrapped, like 58
charaters, then you might try the following line to concatenate
multiple wrapped lines:

$_ .= <PULLIN>, redo unless ( (split//) % 57 );

Sometimes there might exist some lines that have exactly 58 characters,
then the next line should not be wrapped. You should have to find a way
to phase these lines out. For example, in your case, you might need
only lines containing a string 'Unable', then

if ( /Unable/ ) {
$_ .= <PULLIN>, redo unless ( (split//) % 57 );
}

which can concatenate all wrapped lines into one(in $_) and (at least)
the first 58 charaters contain a string 'Unable'..

If you do need the first 58 characters(ignore all the others), then
use "substr($_,0,57)" to extract it after the 'if' statement..

my 2 cents..

Xicheng
 
J

John W. Krahn

jac said:
[snip]

--- code example-----------------------------------------|


open PULLIN,"$in" || die "$in does not exist: $!\n";
open PULLOUT, ">output.txt" || die "$!\n";

Nobody else has pointed this out but the only way that the first open will die
is if $in contains '' or '0' and the second open will never die. The problem
is that the precedence of the '||' operator binds it to the preceding
expression and not to the open operator. You need to either use parentheses:

open( PULLIN, $in ) || die "$in does not exist: $!\n";
open( PULLOUT, ">output.txt" ) || die "$!\n";

Or use the lower precedence 'or' operator:

open PULLIN, $in or die "$in does not exist: $!\n";
open PULLOUT, ">output.txt" or die "$!\n";




John
 
U

usenet

jac said:
A question

This question was multi-posted to perl.beginners. The OP should realize
that cross-posting is sometimes OK, but multi-posting is always a
usenet mortal sin (extremely rude). Multi-posting is a good way to get
killfiled.
 
J

jrbeaman

try:

$_=~ s#Unable to fetch part description for part (.*)#$1 Cannot fetch
Description#gis;
 
I

Ian Wilson

jac said:
This program opens a text file and prints everything except the lines
containing words "adding", "unable", and "PNL". So far it mostly works
like I want it to except that the lines of text that contain the word
"unable" wrap at column "58" to a new line. I need to omit the new line


---INPUT TEXT -----------------------------------------------------|
196315-2 CASTING,VOLUTE,8.5
2-76811 WILL-WA PTAC02 XT70
Unable to fetch part description for part 210-9--4-04120-
210-9--4-04120-
Unable to fetch part description for part 210-9--4-04120-
210-9--4-04120-
231146-76811 S.S. TAG-REF DESIG LABEL KIT
231157-76811 S.S. TAG INSTALLATION KIT

sample output -------------------------------------------------------|
185650-76811-1 LABEL GATHERING KIT, DRVN, CENT
196315-2 CASTING,VOLUTE,8.5
2-76811 WILL-WA PTAC02 XT70
210-9--4-04120-
210-9--4-04120-
231146-76811 S.S. TAG-REF DESIG LABEL KIT
231157-76811 S.S. TAG INSTALLATION KIT
---------------------------------------------------------------------------­--|
In the sample output, the numbers "210-9--4-04120-" are wrapped from a
line above it that contained the word "unable"

Instead of discarding "bad" lines, it is sometimes easier to accept
"good" lines:

$ perl -n -e 'print if /^\d+-\d+\s+/' < t.txt
196315-2 CASTING,VOLUTE,8.5
2-76811 WILL-WA PTAC02 XT70
231146-76811 S.S. TAG-REF DESIG LABEL KIT
231157-76811 S.S. TAG INSTALLATION KIT

YMMV.
 
J

jgraber

Jim Gibson said:
Perl, like most procedural computer languages, does not modify files
directly,

Perl can do read write access to the middle of a file.
I would say that Perl is able to "modify a file directly".
For example, a perl program can gzip files in place,
even if the filesystem is too full to allow a copy of the file,
by doing random access reads and implement write-behind,
then truncate file when done. Easier and more practical
(for non-enormous files) is to gzip to memory,
then overwrite the shorter image to same filename.
so there is no 'delete line' command, and certainly no
'delete line after x' command. A Perl program can read a file in and
write it contents out to the same file or another file. A Perl program
can overwrite a file or truncate a file at some point in the file,
deleting everything in the file after that point. Those are the basic
file manipulations, and the advice you have gotten so far assumes that
you knew this much.

The way to delete a line from a file in Perl is to read the file,

s/The way/A usual way/;
copying the contents to another file, but not copying the parts that
you want to delete. When you are done, you can use the new file as is
or copy it back to the old file.

Or use -i switch to edit "in place"
see perldoc perlrun, search for -i for details
So your program to 'delete line after' boils down to:

1. read a line from the old file
2. write the line to the new file
3. If the next line should be skipped, read a line from the old file
but do not write it to the new file.
4. If there are more lines in the file, go to step 1.

Lines in the file are determined by special characters that appear at
the end of each line.

Ah, there it is, the definition of a line.
If I find a <linemarker> character, and write over it with
null or space, the modified file is the same length as the original,
but has fewer lines. Does that count as "deleting a line"?
A better description might be "unwrapping a wrapped line",
which may (or may) not be what the OP wants.
The exact characters depends upon the platform
and file system you are using. Perl has several different ways of
reading files. See 'perldoc perlop' and search for 'I/O'.

also helpful: perldoc -f open , search for '+<'
 
S

Steve Swift

This question was multi-posted to perl.beginners.

What is the full name of the perl.beginners group, please? (So I can ask
my ISP to carry it). (I searched for all groups containing "begin" but
none of them also contained "perl"
 
D

Dave Weaver

Xicheng Jia said:
$_ .= <PULLIN>, redo unless ( (split//) % 57 );
--------------------------^^^^^^^^^^^^^^^^^^^^^^^^^

You mis-spelled 'if length($_) == 57'

:)
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top