http://www.perlmonks.org?node_id=19127

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I print instead of the following lines :
2000/06/19 15:22:13 NIB1_BSX Communications Alarm + Critical OMC and equipment Connection broken <tag> 2000/06/19 15:22:15 NIB1_MSC Communications Alarm + Critical OMC and equipment Connection broken and up<tag> 2000/06/19 16:03:25 [1]NIB1_MSC.0.0.3.0 Communications Alarm + Minor E1 Port Receive Carrier Loss <tag>
this output : (?)
2000/06/19 15:22:13 NIB1_BSX Communications Alarm + Critical OMC and equipment<tag> 2000/06/19 15:22:15 NIB1_MSC Communications Alarm + Critical OMC and equipment<tag> 2000/06/19 16:03:25 [1]NIB1_MSC.0.0.3.0 Communications Alarm + Minor E1 Port Receive C<tag>
i.e., I want the last field to be only 17 letters long.

Replies are listed 'Best First'.
Re: How do I print only some letters of this string
by nuance (Hermit) on Jun 21, 2000 at 16:38 UTC
    Apologies: I'm aware that these lines are much too long, unfortunately, you can't really see the structure of the data if I wrap them.

    I've reformatted your question and it looks like what you're actually trying is to limit the string to 100 chatracters, like this :

    2000/06/19 15:22:13 NIB1_BSX Communications Alarm + Critical OMC and equipment Connection broken 2000/06/19 15:22:15 NIB1_MSC Communications Alarm + Critical OMC and equipment Connection broken and up 2000/06/19 16:03:25 [1]NIB1_MSC.0.0.3.0 Communications Alarm + Minor E1 Port Receive Carrier Loss this output : (?) 2000/06/19 15:22:13 NIB1_BSX Communications Alarm + Critical OMC and equipment 2000/06/19 15:22:15 NIB1_MSC Communications Alarm + Critical OMC and equipment 2000/06/19 16:03:25 [1]NIB1_MSC.0.0.3.0 Communications Alarm + Minor E1 Port Receive C

    you can use the substr function

    substr($line, 99, 10000000,"");

    should do what you want.

    #!/usr/bin/perl -w use strict; foreach (<DATA>) { substr($_,99,10000000,""); print "$_\n"; } __DATA__ 2000/06/19 15:22:13 NIB1_BSX Communications Alarm + Critical OMC and equipment Connection broken 2000/06/19 15:22:15 NIB1_MSC Communications Alarm + Critical OMC and equipment Connection broken and up 2000/06/19 16:03:25 [1]NIB1_MSC.0.0.3.0 Communications Alarm + Minor E1 Port Receive Carrier Loss
Re: How do I print only some letters of this string
by jlistf (Monk) on Jun 21, 2000 at 18:04 UTC
    i see two ways to answer this question. you could use a regex: (assuming the data is in $_):
    m/(\d{4}/\d{2}/\d{2}\s*?\d{2}:\d{2}:\d{2}\s*?$name\s*Communications Al +arm\s*Critical|Minor\s*?.{17})/; $data = $1;
    that expression may need some debugging, but thats the basic idea. $name can be replaced by something to represent the N1B1,etcs. i don't know what that part of the data is so i'm not sure how to represent it in a regex. second option:
    ($date, $time, $name, $comm, $alarm, $severity, $data) = split ' ',$_, +7; $data = substr ($data,0,17);
    again this might need a little tweaking, but thats the basic idea. there are other ways as well, but these are the two that popped into my head.

      I don't think this regular expression does what you think it does. You have not bounded that | term, so it splits your entire regular expression into two. Also you don't need all the stuff at the start since you aren't actually doing anything with it (storing it etc.)

      You could achieve your goal with:

      /(?:Critical|Minor)\s+(.{0,17})/;

      The (?: term groups without creating a back reference. The \s should not use non-greedy matching, using non-greedy matching here will include leading space in the 17 characters if there is more than one space separating the final two fields. Also we want at least one character at that position so + is better than *. The final field needs to be limited to 17 characters, but if there are less, your expression will fail instead of returing as many as it can find.

      Nuance

        good points all of them. i probably should have done a little more debugging on my answer, but i figured the gist would come across. you could also use ?<= instead of ?: but thats a minor detail.
Re: How do I print only some letters of this string
by draco_iii (Acolyte) on Jun 23, 2000 at 17:18 UTC
    You need to redo this page with <CODE></CODE> tags so that what you are trying to say is comprhenseble(soemthing like that) Also I think that answer maybe in Seekers of Perl Wisdom
Re: How do I print only some letters of this string
by Roy Johnson (Monsignor) on Nov 03, 2003 at 21:53 UTC
    nuance made a couple of sub-optimal choices: for instead of while, and the 4-argument substr to actually change $_ rather than the 3-argument form to get what he wants out of it.
    while (<DATA>) { print substr($_, 0, 100); }