Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I print only some letters of this string

( #19127=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 21, 2000 at 02:50 UTC
Q&A  > input and output


Description:

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 1NIB1_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 1NIB1_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.

Answer: How do I print only some letters of this string
contributed by nuance

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
Answer: How do I print only some letters of this string
contributed by jlistf

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.
Answer: How do I print only some letters of this string
contributed by draco_iii

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

Answer: How do I print only some letters of this string
contributed by Roy Johnson

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); }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (10)
    As of 2015-07-31 00:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (273 votes), past polls