Re: Regex To get the Job Number and Corresponding state

Asad <[email protected]> Wed, 26 Feb 2025 10:11:22 +0530
Newsgroups gmane.comp.lang.perl.beginners
Message-ID <CAG3LsKHk27MSBtpf7T+H7dbqytcs4VsPaZa5eTiw0N_wtCAtTg@mail.gmail.com>
--0000000000001175cc062f0430ec
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thanks a lot.


On Wed, 26 Feb 2025 at 8:55=E2=80=AFAM, Jim Gibson <[email protected]> w=
rote:

> If you are reading a file with normal line endings (e.g., =E2=80=9C\n=E2=
=80=9D), then you
> want to parse each line as =E2=80=9Ckey: value=E2=80=9D using a regular e=
xpression, save
> the job id, current status and current phase values. When you have all
> three, you can check the status and phase values for the values you are
> looking for, print out the job id, and reset all of the variables.
>
> Something like this:
>
> #!/usr/bin/env perl
> use strict;
> use warnings;
>
> my( $status, $phase, $job ) =3D ('')x3;
>
> for my $line (<DATA>) {
>         chomp($line);
>         print "  $line\n";
>         if ( $line =3D~ /([^:]+):\s*(.*)/ ) {
>                 my( $key, $val ) =3D ($1,$2);
>                 if( $key eq 'Job ID' ) {
>                         $job =3D $val;
>                 }elsif( $key eq 'Current status' ) {
>                         $status =3D $val;
>                 }elsif( $key eq 'Current Phase' ) {
>                         $phase =3D $val;
>                 }
>
>                 if( $job && $status && $phase ) {
>                         if( ($status eq 'SUCCEEDED') && ($phase eq
> q("ZDM_SETUP_TGT")) ) {
>                                 print "Job $job is complete\n";
>                         }
>                         ( $status, $phase, $job ) =3D ('')x3;
>
>                 }
>         }
> }
> __DATA__
> Job ID: 52
> User: zdmuser
> Client: zdmhost
> Job Type: "EVAL"
> Current status: SUCCEEDED
> Current Phase: "ZDM_SETUP_TGT"
> Job ID: 53
> Current status: FAIL
> Current Phase: END
>
>
> > On Feb 25, 2025, at 8:49=E2=80=AFAM, Asad <[email protected]> wr=
ote:
> >
> > Hi All,
> >
> > I have a requirement to use regex  , find the Job ID for the Current
> status: SUCCEEDED and Current Phase: "ZDM_SETUP_TGT" from a file
> > which has data in the format gives below :
> >
> > Job ID: 52
> > User: zdmuser
> > Client: zdmhost
> > Job Type: "EVAL"
> > ...
> > Current status: SUCCEEDED
> > Current Phase: "ZDM_SETUP_TGT"
> >
> >
> > Thanks,
>
>

--0000000000001175cc062f0430ec
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div>Thanks a lot.</div><div><br></div><div><br><div class=3D"gmail_quote g=
mail_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, 26 Feb =
2025 at 8:55=E2=80=AFAM, Jim Gibson &lt;<a href=3D"mailto:jimsgibson@gmail.=
com">[email protected]</a>&gt; wrote:<br></div><blockquote class=3D"gmai=
l_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-lef=
t-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)">If you a=
re reading a file with normal line endings (e.g., =E2=80=9C\n=E2=80=9D), th=
en you want to parse each line as =E2=80=9Ckey: value=E2=80=9D using a regu=
lar expression, save the job id, current status and current phase values. W=
hen you have all three, you can check the status and phase values for the v=
alues you are looking for, print out the job id, and reset all of the varia=
bles.<br>
<br>
Something like this:<br>
<br>
#!/usr/bin/env perl<br>
use strict;<br>
use warnings;<br>
<br>
my( $status, $phase, $job ) =3D (&#39;&#39;)x3;<br>
<br>
for my $line (&lt;DATA&gt;) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 chomp($line);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print &quot;=C2=A0 $line\n&quot;;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if ( $line =3D~ /([^:]+):\s*(.*)/ ) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 my( $key, $val ) =
=3D ($1,$2);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if( $key eq &#39;Jo=
b ID&#39; ) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 $job =3D $val;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }elsif( $key eq &#3=
9;Current status&#39; ) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 $status =3D $val;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }elsif( $key eq &#3=
9;Current Phase&#39; ) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 $phase =3D $val;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if( $job &amp;&amp;=
 $status &amp;&amp; $phase ) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 if( ($status eq &#39;SUCCEEDED&#39;) &amp;&amp; ($phase eq q(&qu=
ot;ZDM_SETUP_TGT&quot;)) ) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print &quot;Job $job is complete\n&q=
uot;;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 ( $status, $phase, $job ) =3D (&#39;&#39;)x3;=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>
}<br>
__DATA__<br>
Job ID: 52<br>
User: zdmuser<br>
Client: zdmhost<br>
Job Type: &quot;EVAL&quot;<br>
Current status: SUCCEEDED<br>
Current Phase: &quot;ZDM_SETUP_TGT&quot;<br>
Job ID: 53<br>
Current status: FAIL<br>
Current Phase: END<br>
<br>
<br>
&gt; On Feb 25, 2025, at 8:49=E2=80=AFAM, Asad &lt;<a href=3D"mailto:asad.h=
[email protected]" target=3D"_blank">[email protected]</a>&gt; wrot=
e:<br>
&gt; <br>
&gt; Hi All,<br>
&gt; <br>
&gt; I have a requirement to use regex=C2=A0 , find the Job ID for the Curr=
ent status: SUCCEEDED and Current Phase: &quot;ZDM_SETUP_TGT&quot; from a f=
ile <br>
&gt; which has data in the format gives below :<br>
&gt; <br>
&gt; Job ID: 52<br>
&gt; User: zdmuser<br>
&gt; Client: zdmhost<br>
&gt; Job Type: &quot;EVAL&quot;<br>
&gt; ...<br>
&gt; Current status: SUCCEEDED<br>
&gt; Current Phase: &quot;ZDM_SETUP_TGT&quot;<br>
&gt; <br>
&gt; <br>
&gt; Thanks,<br>
<br>
</blockquote></div></div>

--0000000000001175cc062f0430ec--