CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to isolate data elements with a data block with (\n) newlines????

 



preston3271
Novice

Nov 26, 2012, 2:02 PM

Post #1 of 7 (1583 views)
How to isolate data elements with a data block with (\n) newlines???? Can't Post

Hello, Perl Guru's

After several days of efforts to figure out how to gather the needed information from this block of data, I realized that I need help.

The data is as follows:


Code
__DATA__ 
RequestID: 44529658-5293-7561-0514-ef2b44ac8545
Context:
CN: 82d246f6-51ff-7561-1702-ae217f13e8af
Nodename: lnmtcodcld20
IPAddress: 10.109.240.184
Platform: Solaris 5.10, CPU: Sparc
InstallType: Auto
TimeReceived: 10/26/12 14:42:05 MDT

RequestID: 5bacd03e-5293-7561-04ff-ba57719eb9f2
Context:
CN: 82c0a1c6-51ff-7561-1a7f-cb57fc0b994f
Nodename: lnmtcodcld10
IPAddress: 10.109.240.181
Platform: Solaris 5.10, CPU: Sparc
InstallType: Auto
TimeReceived: 10/26/12 14:42:06 MDT

RequestID: 6dc42aaa-48b7-7561-0a8b-8e0c3919ec2c
Context:
CN: e860e9c2-48af-7561-18e5-a5791ab456c6
Nodename: lnmtcodzcba03.scc911.com
IPAddress: 10.100.113.32
Platform: Linux 2.6.32, CPU: x64
InstallType: Auto
TimeReceived: 10/25/12 08:35:27 MDT

RequestID: c4193482-6a49-7561-150b-8b0507771399
Context:
CN: df58e362-6a41-7561-11aa-9f8c4f608300
Nodename: LMV08-DC02.corp.intrado.pri
IPAddress: 10.100.28.1
Platform: Windows 6.1, CPU: x64
InstallType: Auto
TimeReceived: 10/29/12 15:07:33 MDT

RequestID: e0e00eb2-48be-7561-013b-e012ec75886d
Context:
CN: 2e122fe0-48be-7561-053a-a2b50066dba0
Nodename: lnmtcodzcba04.scc911.com
IPAddress: 10.100.113.33
Platform: Linux 2.6.32, CPU: x64
InstallType: Auto
TimeReceived: 10/25/12 08:40:47 MDT


root@somesystem>/root #

What I need is to isolate the RequestID and the Nodename if the node in found within this set of data. I have tried splitting on the blank lines and then removing the the (\n) newlines and replacing them ":" using a join and then putting them into an array with no success of what I want to isolate.

I even trying to one step my results by created reference via a pattern match as follows:


Code
#!C:\strawberry\perl\bin\perl 
while (<DATA>) {
if(m#
^\s*$
(\d):\s+(\d)\n
(\d):\s+(\d)\n
(\d):\s+(\d)\n
(\d):\s+(\d)\n
(\d):\s+(\d)\n
(\d):\s+(\d)\n
(\d):\s+(\d)\n
(\d):\s+(\d)\n
^\s*$
#)
{

print "$1 \n";
}

}

__DATA__

RequestID: 44529658-5293-7561-0514-ef2b44ac8545
Context:
CN: 82d246f6-51ff-7561-1702-ae217f13e8af
Nodename: lnmtcodcld20
IPAddress: 10.109.240.184
Platform: Solaris 5.10, CPU: Sparc
InstallType: Auto
TimeReceived: 10/26/12 14:42:05 MDT

RequestID: 5bacd03e-5293-7561-04ff-ba57719eb9f2
Context:
CN: 82c0a1c6-51ff-7561-1a7f-cb57fc0b994f
Nodename: lnmtcodcld10
IPAddress: 10.109.240.181
Platform: Solaris 5.10, CPU: Sparc
InstallType: Auto
TimeReceived: 10/26/12 14:42:06 MDT

RequestID: 6dc42aaa-48b7-7561-0a8b-8e0c3919ec2c
Context:
CN: e860e9c2-48af-7561-18e5-a5791ab456c6
Nodename: lnmtcodzcba03.scc911.com
IPAddress: 10.100.113.32
Platform: Linux 2.6.32, CPU: x64
InstallType: Auto
TimeReceived: 10/25/12 08:35:27 MDT

RequestID: aed765f4-f66b-7561-17cb-c28bb30ac813
Context:
CN: 48cdb95e-f655-7561-0cf2-813ab27c6c99
Nodename: lnmtcodcala20.scc911.com
IPAddress: 10.109.240.125
Platform: Linux 2.6.18, CPU: x64
InstallType: Auto
TimeReceived: 11/16/12 10:07:04 MST

RequestID: c4193482-6a49-7561-150b-8b0507771399
Context:
CN: df58e362-6a41-7561-11aa-9f8c4f608300
Nodename: LMV08-DC02.corp.intrado.pri
IPAddress: 10.100.28.1
Platform: Windows 6.1, CPU: x64
InstallType: Auto
TimeReceived: 10/29/12 15:07:33 MDT

RequestID: e0e00eb2-48be-7561-013b-e012ec75886d
Context:
CN: 2e122fe0-48be-7561-053a-a2b50066dba0
Nodename: lnmtcodzcba04.scc911.com
IPAddress: 10.100.113.33
Platform: Linux 2.6.32, CPU: x64
InstallType: Auto
TimeReceived: 10/25/12 08:40:47 MDT


or


Code
while (<DATA>) { 
chomp;
if(m#

\s*(\d+):\s+(\d+)(\d+):(\d+):\s+(\d+)(\d+):\s+(\d+)\s*
#)
{

print "$1 \n";
}
print $_;
}

__DATA__
. . .


I even read up on using a MAP but the only example close to what I was doing was going in the opposite direction.

Please enlighten me in my efforts to isolate the RequestID and Nodename within the appropriate data block.

Preston


Laurent_R
Veteran / Moderator

Nov 26, 2012, 2:46 PM

Post #2 of 7 (1581 views)
Re: [preston3271] How to isolate data elements with a data block with (\n) newlines???? [In reply to] Can't Post

Perhaps something like this:


Code
my ($request, $node) 
while (<DATA>) {
next unless /^RequestID/ or /^Nodename/;
chomp;
my($name, $id) = split /:\s+/, $_;
$request = $id if $name eq "RequestID";
$node = $id if $name eq "Nodename";
if (defined $request and defined $node) {
print "RequestID $request on nodename $node \n";
($request, $node) = (undef, undef);
}
}



preston3271
Novice

Nov 26, 2012, 3:20 PM

Post #3 of 7 (1577 views)
Re: [Laurent_R] How to isolate data elements with a data block with (\n) newlines???? [In reply to] Can't Post

So . . .


Code
  

#!C:\strawberry\perl\bin\perl
my ($request, $node);
while (<DATA>) {
next unless /^RequestID/ or /^Nodename/;
chomp;
my($name, $id) = split /:\s+/, $_;
$request = $id if $name eq "RequestID";
$node = $id if $name eq "Nodename";
if (defined $request and defined $node) {
print "RequestID $request on nodename $node \n";
($request, $node) = (undef, undef);
}
}


__DATA__

RequestID: 44529658-5293-7561-0514-ef2b44ac8545
Context:
CN: 82d246f6-51ff-7561-1702-ae217f13e8af
Nodename: lnmtcodcld20

RequestID: 42343436-5293-7561-0514-ef2b44ac8545
Context:
CN: 82d246f6-51ff-7561-1702-ae217f13e8aa
Nodename: lnmtcodcld10



Dang! It works! I just don't get it yet, but it works. I am still studying your perl-fu.

Perl-fu by Example
-----------------------------

line 2: Create a local anonymous array

line 3: while looping the <DATA>

line 4: Next line unless either RequestID or Nodename at start of the line

line 5: Chomp or remove "\n" newlines for each $_ entry

line 6: Create another anonymous local array and set it equal to a Perl split matching on
":" and space(s) (/:\s+/) with a >> comma which I don't understand << followed by the $_
short for return whatever from that line.

line 7: $request = $id . . . okay my Perl-fu got weak after the local array and the split. Can you enlighten me on the split, comma, and $_ please.


preston3271
Novice

Nov 26, 2012, 3:28 PM

Post #4 of 7 (1575 views)
Re: [preston3271] How to isolate data elements with a data block with (\n) newlines???? [In reply to] Can't Post

Duh! Okay, I get the /:\s+/, $_ match m/ / on the current line.

I don't get the $name and $id happening here . . .


Code
  

my($name, $id) = split /:\s+/, $_;
$request = $id if $name eq "RequestID";
$node = $id if $name eq "Nodename";



preston3271
Novice

Nov 26, 2012, 3:39 PM

Post #5 of 7 (1574 views)
Re: [preston3271] How to isolate data elements with a data block with (\n) newlines???? [In reply to] Can't Post

Okay, my($name, $id) = split /:\s+/, $_;

line 6: match whatever before or after the ": ".

line 7: This is absolutely clever.

I am still working to mentalize the line 7 and line 8 but I get it . . . sorta.


Laurent_R
Veteran / Moderator

Nov 27, 2012, 12:26 AM

Post #6 of 7 (1557 views)
Re: [preston3271] How to isolate data elements with a data block with (\n) newlines???? [In reply to] Can't Post

 
Hi,

there is nothing really mysterious in my code suggestion. Here is the code thougoughly commented.



Code
my ($request, $node); # declare two variables 
while (<DATA>) {
next unless /^RequestID/ or /^Nodename/; # discard useless lines
chomp; # removes trailing \n
my($name, $id) = split /:\s+/, $_; # splits the line on colon (:) followed by any number of spaces
# note that ($name, $id) is not really an anonymous array, but simply a list of two fields
# we have two types of line, so $name may contain RequestID or Nodename, and $id contains the corresponding value
# the next two lines just check what there is in the first field to decide what to do with the $id value
$request = $id if $name eq "RequestID"; # if the line was a RequestID line...
$node = $id if $name eq "Nodename"; # if it was a Nonename line
if (defined $request and defined $node) { # the only slightly tricky part: we print the result only if we have met both types of lines
print "RequestID $request on nodename $node \n";
($request, $node) = (undef, undef); # reset the two variables to undef to make "defined" test possible
}
}

Please don't hesitate to ask if you still don't understand something.


(This post was edited by Laurent_R on Nov 27, 2012, 4:06 AM)


preston3271
Novice

Nov 27, 2012, 9:52 AM

Post #7 of 7 (1549 views)
Re: [Laurent_R] How to isolate data elements with a data block with (\n) newlines???? [In reply to] Can't Post

Thanks, I found my issue with my last question. I was using the "==" instead of the "eq" for grab out the desired elements I wanted. Thank you very much.

#!C:\strawberry\perl\bin\perl

@final=();
my ($request, $node);
while (<DATA>) {
next unless /^RequestID/ or /^Nodename/;
chomp;
my($name, $id) = split /:\s+/, $_;
$request = $id if $name eq "RequestID";
$node = $id if $name eq "Nodename";

if(lnmtcodcld20 eq $node)
{
print "Yes: $node \n";
push(@final, $node, $request);
} else { print "No Match: $node \n"; }

if (defined $request and defined $node) {
print "RequestID $request on Nodename $node \n";

($request, $node) = (undef, undef);
}
}

print "@final[0] and @final[1] \n";


__DATA__

RequestID: 44529658-5293-7561-0514-ef2b44ac8545
Context:
CN: 82d246f6-51ff-7561-1702-ae217f13e8af
Nodename: lnmtcodcld20
IPAddress: 10.109.240.184
Platform: Solaris 5.10, CPU: Sparc
InstallType: Auto
TimeReceived: 10/26/12 14:42:05 MDT

RequestID: 5bacd03e-5293-7561-04ff-ba57719eb9f2
Context:
CN: 82c0a1c6-51ff-7561-1a7f-cb57fc0b994f
Nodename: lnmtcodcld10
IPAddress: 10.109.240.181
Platform: Solaris 5.10, CPU: Sparc
InstallType: Auto
TimeReceived: 10/26/12 14:42:06 MDT

RequestID: 6dc42aaa-48b7-7561-0a8b-8e0c3919ec2c
Context:
CN: e860e9c2-48af-7561-18e5-a5791ab456c6
Nodename: lnmtcodzcba03.scc911.com
IPAddress: 10.100.113.32
Platform: Linux 2.6.32, CPU: x64
InstallType: Auto
TimeReceived: 10/25/12 08:35:27 MDT

RequestID: aed765f4-f66b-7561-17cb-c28bb30ac813
Context:
CN: 48cdb95e-f655-7561-0cf2-813ab27c6c99
Nodename: lnmtcodcala20.scc911.com
IPAddress: 10.109.240.125
Platform: Linux 2.6.18, CPU: x64
InstallType: Auto
TimeReceived: 11/16/12 10:07:04 MST

RequestID: c4193482-6a49-7561-150b-8b0507771399
Context:
CN: df58e362-6a41-7561-11aa-9f8c4f608300
Nodename: LMV08-DC02.corp.intrado.pri
IPAddress: 10.100.28.1
Platform: Windows 6.1, CPU: x64
InstallType: Auto
TimeReceived: 10/29/12 15:07:33 MDT

RequestID: e0e00eb2-48be-7561-013b-e012ec75886d
Context:
CN: 2e122fe0-48be-7561-053a-a2b50066dba0
Nodename: lnmtcodzcba04.scc911.com
IPAddress: 10.100.113.33
Platform: Linux 2.6.32, CPU: x64
InstallType: Auto
TimeReceived: 10/25/12 08:40:47 MDT

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives