Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
How could I gather the variables from a multi-part if with RegEx's , or do I need to break it down into separate parts?
while(<IFILE>) { if ((@LINE =~ /#\s+(\w+)\s+(\d+).+/) && (@LINE =~ /!\s+(\w+)\s+(\d+ +).+/)) { $Word=$1; $TimeStamp=$2; $Word2=$3; $TimeStamp2=$4; print "X $Word X $TimeStamp X $Word2 X $TimeStamp2 x"; } }
Thanx, all help much appreciated.
# add 1362072030 dc=ifdsgroup,dc=com dn: uid=nce9834xdho,ou=user,ou=nce,ou=prod ! port 1362 dc=isgrp,dc=com changetype: add ifastCreateDate: 1362072030649 objectClass: account objectClass: ifastUser objectClass: top ifastModifyDate: 1362072030649 uid: nce9834xdho cn: nce9834xdho ifastProducts: WEB ifastGivenName: Omar ifastLanguage: en ifastFailedLogons: 0 ifastLastLogon: 15764 ifastEmailAddr: omar.hafez@td.com ifastSurname: Hafez structuralObjectClass: account entryUUID: e587875c-1616-1032-93e5-4b7d087c5768 createTimestamp: 20130228172030Z modifyTimestamp: 20130228172030Z # end add 1362072030 # add 1362072030 dc=ifdsgroup,dc=com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # end add 1362072030
The suggestion by choroba seems to work, and is part of the solution, but I can't seem to get it working with more than one instance of it in the same if statement: Works:
undef $/; $/="\n\n"; while(<IFILE>) { my @RESULT=(); my @LINES=split (/\n/, $_); if ((grep /objectClass:\s+account/, @LINES) && (grep /objectClass:\s+ifastUser/, @LINES) #&& (my ( $PRODS ) = /ifastProducts:\s+(.+)\s*$/, $_) && (my ( $USER ) = /dn: uid=(\w+?),.+/, $_)) { push(@RESULT, $USER); #push(@RESULT, $PRODS, $USER); print join "|", @RESULT; #print OFILE join "|", @RESULT; #print OFILE "\n"; print "\n"; } }
But this does not:
undef $/; $/="\n\n"; while(<IFILE>) { my @RESULT=(); my @LINES=split (/\n/, $_); if ((grep /objectClass:\s+account/, @LINES) && (grep /objectClass:\s+ifastUser/, @LINES) && (my ( $PRODS ) = /ifastProducts:\s+(.+)\s*$/, $_) && (my ( $USER ) = /dn: uid=(\w+?),.+/, $_)) { #push(@RESULT, $USER); push(@RESULT, $PRODS, $USER); print join "|", @RESULT; #print OFILE join "|", @RESULT; #print OFILE "\n"; print "\n"; } }

In reply to Get variables from if with regex by Saved

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found