Re: Regex To get the Job Number and Corresponding state
[email protected] (Asad) Wed, 26 Feb 2025 10:11:22 +0530
| Newsgroups | 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 <<a href=3D"mailto:jimsgibson@gmail.= com">[email protected]</a>> 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 ('')x3;<br> <br> for my $line (<DATA>) {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 chomp($line);<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 print "=C2=A0 $line\n";<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 'Jo= b ID' ) {<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 = 9;Current status' ) {<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 = 9;Current 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 $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 &&= $status && $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 'SUCCEEDED') && ($phase eq q(&qu= ot;ZDM_SETUP_TGT")) ) {<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 "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 ('')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: "EVAL"<br> Current status: SUCCEEDED<br> Current Phase: "ZDM_SETUP_TGT"<br> Job ID: 53<br> Current status: FAIL<br> Current Phase: END<br> <br> <br> > On Feb 25, 2025, at 8:49=E2=80=AFAM, Asad <<a href=3D"mailto:asad.h= [email protected]" target=3D"_blank">[email protected]</a>> wrot= e:<br> > <br> > Hi All,<br> > <br> > I have a requirement to use regex=C2=A0 , find the Job ID for the Curr= ent status: SUCCEEDED and Current Phase: "ZDM_SETUP_TGT" from a f= ile <br> > which has data in the format gives below :<br> > <br> > Job ID: 52<br> > User: zdmuser<br> > Client: zdmhost<br> > Job Type: "EVAL"<br> > ...<br> > Current status: SUCCEEDED<br> > Current Phase: "ZDM_SETUP_TGT"<br> > <br> > <br> > Thanks,<br> <br> </blockquote></div></div> --0000000000001175cc062f0430ec--