Re: Regex To get the Job Number and Corresponding state

Jim Gibson <[email protected]> Tue, 25 Feb 2025 19:25:16 -0800
Newsgroups gmane.comp.lang.perl.beginners
Message-ID <[email protected]>
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 expression, 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;
		}
	=09
		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;		=09=

		}
	}
}
__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
 =20

> On Feb 25, 2025, at 8:49=E2=80=AFAM, Asad <[email protected]> =
wrote:
>=20
> Hi All,
>=20
> 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=20
> which has data in the format gives below :
>=20
> Job ID: 52
> User: zdmuser
> Client: zdmhost
> Job Type: "EVAL"
> ...
> Current status: SUCCEEDED
> Current Phase: "ZDM_SETUP_TGT"
>=20
>=20
> Thanks,


-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/