substitution fails for long line

P

Pranav Agarwal

Hi All,

Any advise, suggestion on the same will be appretiated!

I have a file that contains a very long line, When I try to open it in vi
editor, I am not able to open the full file, I get the error "line too long"
The length of the long line is 23646.

When I try to run the sed command like this
%/usr/bin/sed -e 's/\(.*\)/\1/' longfile
I get the error "Output line too long."
Above error gets solved when I use /usr/xpg4/bin/sed That is
%/usr/xpg4/bin/sed -e 's/\(.*\)/\1/' longfile
runs good.

The problem I am facing is when i try to run my a.pl i.e.
open(FH,"$ARGV[0]");
while(<FH>){
$x=$_;
$len = length($_);
print ("$len\n");
$x =~ s/(\s*)(nothingtomatch)/$1/g;
print $x;
}
My perl program hangs when the value for $_ is that particular long line.
%perl a.pl x.1
20
<H3>References</H3>
23646
^C (!!! HANGED !!!)
%

Thanks a lot in advance for any suggestions.

Regards,
-Pranav.
 
A

A. Sinan Unur

Hi All,

Any advise, suggestion on the same will be appretiated!

I have a file that contains a very long line, When I try to open it in
vi editor, I am not able to open the full file, I get the error "line
too long" The length of the long line is 23646.

What is preventing you from fixing the lines?
 
T

Tad McClellan

Pranav Agarwal said:
open(FH,"$ARGV[0]");

You should always, yes *always*, check the return value from open():

open(FH, $ARGV[0]) or die "could not open '$ARGV[0]' $!";


Have you seen this Perl FAQ?

What's wrong with always quoting "$vars"?


perl will do the open()ing for you. said:
while(<FH>){
$x=$_;


If you want it in $x, then put it in $x straightaway

while ( my $x = <FH> ) {
 
G

gnari

[snipped problem with long line]

have you tried shortening the line to see what makes this happen?
find the shortest line this happens to.
did changing the s/// have any effect?
$x =~ s/(\s*)(nothingtomatch)/$1/g;

is this literally the code you used ?

are you using an operating system/terninal that does not
tolerate all characters as output? (think ctrl-S)

perl does not have any difficulty with long lines.

gnari
 
C

ctcgag

Pranav Agarwal said:
The problem I am facing is when i try to run my a.pl i.e.
open(FH,"$ARGV[0]");
while(<FH>){
$x=$_;
$len = length($_);
print ("$len\n");
$x =~ s/(\s*)(nothingtomatch)/$1/g;
print $x;
}
My perl program hangs when the value for $_ is that particular long line.
%perl a.pl x.1
20
<H3>References</H3>
23646
^C (!!! HANGED !!!)

Is it spinning the CPU or is just sleeping?

What is the exact regex you are matching? If the regex engine
has to march back and forth over a very long line a very high number
of times, it may take a very long time to do it.

Xho
 
P

Pranav Agarwal

Thanks for the reply!
You were correct, this regex completes after taking a lot of time more than
1 hour.
my actual regular expression was taking around 4 hrs to complete.
You input really helped me to solve the problem.
In a part of my regular expression I was matching with the white spaces at
multiple places, I have removed that from the original regex and implemented
the same in new regex for the subset of the the sentence.

Thanks again for your useful input.

Regards,
-Pranav.


Pranav Agarwal said:
The problem I am facing is when i try to run my a.pl i.e.
open(FH,"$ARGV[0]");
while(<FH>){
$x=$_;
$len = length($_);
print ("$len\n");
$x =~ s/(\s*)(nothingtomatch)/$1/g;
print $x;
}
My perl program hangs when the value for $_ is that particular long line.
%perl a.pl x.1
20
<H3>References</H3>
23646
^C (!!! HANGED !!!)

Is it spinning the CPU or is just sleeping?

What is the exact regex you are matching? If the regex engine
has to march back and forth over a very long line a very high number
of times, it may take a very long time to do it.

Xho
 

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

Latest Threads

Top