Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: Re: Re: Can PERL know a line without matching?

by brassmon_k (Sexton)
on Jul 30, 2001 at 20:39 UTC ( #100900=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Can PERL know a line without matching?
in thread Can PERL know a line without matching?

OK that worked for the lines printing. Thank you, I understand it. I just didn't know that PERL would know what lines to print if you split on the newline. I'm very unfamiliar with multi-line as I said. I'm having problems with one last thing though. I'll show you a small portion of the text I'm working with and what I want to do is limit what the script prints. I'm sorry you guys told me exactly what I wanted to know but I started off with a simple explanation, well as easy as I could so I could work on the last part later. I'll show you the text I'm going through then I'll show you how I modified Bike Nomads script with your idea and made it work.

mSOriginatingSMSinSMSIWMSC Call Identification Number: 10275084 Record Sequence Number: 2205369 Exchange Identity: MAPP01E 0117802 MSC Identification: TON: 1 NPI: 1 MSISDN: 19207079800 Calling Party Number: TON: 1 NPI: 1 MSISDN: 19207029659 Date for Start of Charge: 01/07/18 Time for Start of Charge: 00:14:33 Traffic Activity Code: TeleService Code: x'22 Service Centre Address: TON: 1 NPI: 1 MSISDN: 14147139800 Charged Party: 0 Miscellaneous Information: NULL MSTerminating Call Identification Number: 10275019 Related Call Number: 10275004 Record Sequence Number: 2205370 Exchange Identity: MAPP01E 0117802 MSC Identification: TON: 1 NPI: 1 MSISDN: 19207079800 Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'4F07 Incoming Route: MAD01TI Outgoing Route: BAPL01O Calling Party Number: TON: 4 NPI: 1 MSISDN: 6082572086 Called Party Number: TON: 1 NPI: 1 MSISDN: 16084466501 IMSI: 310640010049547 IMEI: NULL Mobile Station Roaming Number: TON: 1 NPI: 1 MSISDN: 16084469974 Redirecting Number: NULL Redirection Counter: 0 Original Called Number: NULL Date for Start of Charge: 01/07/18 Time for Start of Charge: 00:14:33 Chargeable Duration: 00:00:00 Traffic Activity Code: *020 TeleService Code: x'11 Bearer Service Code: NULL Internal Cause and Loc: LOCATION: 2 CAUSE: 61 Time from Register Seizure to Start of Charging: 00:00:18 Time for Stop of Charging: 00:14:33 Interruption Time: 00:00:00 Type of Calling Subscriber: 1 Disconnecting Party: 2 Charged Party: 1 Fault Code: NULL eosInfo: x'0 Call Position: 2 Miscellaneous Information: NULL Restart During Call: NULL Restart Between Disc and Output: NULL Origin for Charging: x'1 Cell ID for Last Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'4F1D Location Number: TON: 1 NPI: 1 MSISDN: 14147089800 Output for Subscriber: NULL Last Partial Output: NULL Partial Output Rec Num: NULL Regional Service Used: NULL Region Dependent Charging Origin: NULL Transparency Indicator: NULL dTMFUsed: NULL Tariff Class: x'A Tariff Switch Indicator: 0 SS Code: NULL ICI Ordered: NULL
Now for the script
#!/usr/bin/perl chomp($msisdn = <STDIN>); $/ = ""; # read paragraphs while ($para = <>) { $lastHeading; @lines = split(/\n/, $para); if (@lines == 1) # A Heading { $lastHeading = $lines[0]; next; } my $hitMe; if ($msisdn && $lastHeading eq "MSTerminating") { #if ($lastHeading eq "MSTerminating") { print $lines[13]; # twelth line print $lines[2]; # third line print $lines[5]; # sixth line print $lines[4]; # fifth line } elsif ($lastHeading eq "MSORIGINATING") { $hitMe = $lines[5]; # sixth line } elsif ($lastHeading eq "mSOriginatingSMSinSMSIWMSC") { print $lines[5]; } }
As you can see from the script what I'm trying to do is have the Title of the record block and the lines I specifiy per block to print only if the script finds the $msisdn in any of the blocks. That's the phone number or 10th line #11th if you count Title "MSTerminating" as line 1 for a block. At the end the number is 6082572086. So for example since that is the number I'm interested in, the only record block which contains the number I'm searching for is "MSTerminating" and not any of the other record blocks so I'd want it to print off only the lines for "MSTerminating" as that is the only place the number was found.

I was thinking of doing "if (grep, $msisdn) {" then your if The && operator I used didn't seem to work. The && operator confuses me as to me the logic says if this and that do what I tell you to. It hates me though. I was thinking of doing a couple variations but you guys have helped me with the hardest part in my opinion because I didn't know multi-line. I'll remember what you guys taught me so I'm not back here nagging you guys all the time. I can write regex's to search inside files for single lines and search files in directories and "printem"(Possible new print statement? LOL) or pull them but this was something different and I read the books but half the stuff was reading greek to me cuz I never had to use it. Some of the stuff I think you just gotta use it to understand it.

Well please offer your advice on the last portion.
Thankyou lots,
Dave


Comment on Re: Re: Re: Re: Can PERL know a line without matching?
Select or Download Code
Re: Re: Re: Re: Re: Can PERL know a line without matching?
by da (Friar) on Jul 30, 2001 at 23:57 UTC
    The 'and' operator, &&, works very simply. It takes two arguments. It evaluates the first argument, and if it's successful, it evaluates the second argument. If that's successful, it returns '1'.

    I hope that helps.

    I think what you want for your 'if' statement is something like:

    if (($para =~ /$msisdn/) && ($lastHeading eq "MSTerminating")) {
    That is, if $para includes the text in $msisdn, and $lastheading exactly equals "MSTerminating", then do the following.

    One stylistic point; you should definitely think about use strict and use warnings' and you should definitely indent your code inside loops, because it prevents a number of confusions later.

    HTH. -Daniel

    ___ -DA > perl -MPOSIX -le '$ENV{TZ}="EST";print ctime(1000000000)' Sat Sep 8 20:46:40 2001

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://100900]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-28 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls