substitution fails for long line

Discussion in 'Perl Misc' started by Pranav Agarwal, Feb 9, 2004.

  1. 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.
    Pranav Agarwal, Feb 9, 2004
    #1
    1. Advertising

  2. "Pranav Agarwal" <> wrote in
    news:19JVb.4$:

    > 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?


    --
    A. Sinan Unur
    (reverse each component for email address)
    A. Sinan Unur, Feb 9, 2004
    #2
    1. Advertising

  3. Pranav Agarwal <> wrote:

    > 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"?


    If you read from <>, perl will do the open()ing for you.


    > while(<FH>){
    > $x=$_;



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

    while ( my $x = <FH> ) {


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 9, 2004
    #3
  4. Pranav Agarwal

    gnari Guest

    "Pranav Agarwal" <> wrote in message
    news:19JVb.4$...

    [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
    gnari, Feb 9, 2004
    #4
  5. Pranav Agarwal

    Guest

    "Pranav Agarwal" <> wrote:
    >
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    , Feb 10, 2004
    #5
  6. 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.


    <> wrote in message
    news:20040209202603.919$...
    > "Pranav Agarwal" <> wrote:
    > >
    > > 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
    >
    > --
    > -------------------- http://NewsReader.Com/ --------------------
    > Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    Pranav Agarwal, Feb 11, 2004
    #6
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    661
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,179
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    463
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    784
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,779
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page