Operating System - HP-UX
1752795 Members
6076 Online
108789 Solutions
New Discussion юеВ

Re: How to just extract expiry date and server name from the following file

 
SOLVED
Go to solution
pareshan
Regular Advisor

Re: How to just extract expiry date and server name from the following file

Thanks Very Much Hein, I really appreciate your efort and time.
But I cant use with any other data than the sample I have provided before, it just gives me blank for other data. And also it doesnt give me all the data from the file just it gives some columns. I need all the data from the file in output

I am writting here the both data the one working and the not working
---Working----
cat extracted-data.dat
# SyncSort UNIX 08/31/2004 08/31/2009 "astro" "9000/800" 64 * * * X280-D-1
# SyncSort UNIX 08/16/2001 08/15/2011 "benji" "9000/800" 64 * * * V876-J-2
# SyncSort UNIX 03/30/2006 03/29/2011 "bhpbob10" "9000/800" 32 * * * X366-E-1
# SyncSort UNIX 02/24/2009 03/26/2009 "" "" * * * * * * * * * * * ""
extract.pl extracted-data.datThe servers expired already or will expire within 7 days

2009/03/26"" " "" "

The servers will expire between 8 to 30th day


The servers will expire after 30th day

2009/08/31"astro" "astro"
2011/03/29"bhpbob10" "bhpbob10"
2011/08/15"benji" "benji"

----not working----
now, the other data (actual data) which I need to use script for
cat required.dat

astro 08/31/2004 08/31/2009 permanent Number of CPUs : 64
benji 08/16/2001 08/15/2011 permanent Number of CPUs : 64
bhpbob10 03/30/2006 03/29/2011 permanent Number of CPUs : 16
bhptlg82 02/24/2009 03/26/2009 temporary Number of CPUs : 16

./extract.pl required.dat

The servers expired already or will expire within 7 days


The servers will expire between 8 to 30th day


The servers will expire after 30th day

It just gives me blank nothing at all. I have tried alot whole 2 days try to make it work with the real data I need but I am not able to.
Note:: I need all the data which is in that required.dat file in output just need to categories in three list as I have told before

Could you help me out plz. or anyone else based on the script I am using below or that posted by hein just right above this one. I have just did minor changes in that but working fie

cat extract.pl
#!/usr/local/dazel/bin/perl

print "\n The servers expired already or will expire within 7 days\n\n";
# load a 7 day cutoff marker

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 7 * 86400);
$ymd = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
$mdy = sprintf qq(%02d/%02d/%d), $mon+1, $mday, $year+1900;
$ordered{qq($ymd The servers will expire between 8 to 30th day \n)} = "\n";

# calculate a 30 day cutoff time

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 30 * 86400);
# $ymd_30 = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
$ymd = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
$mdy = sprintf qq(%02d/%02d/%d), $mday, $mon+1, $year+1900;
# $ordered{qq($ymd ---- 30 day cut-off ----)} = $mdy;
$ordered{qq($ymd The servers will expire after 30th day \n)} ="\n";

while (<>) {

if ( m{(\d\d)/(\d\d)/(\d{4})\s+(".+?")} ) {

$yyyymmdd = $3.$1.$2.' '.$4;
#$ordered{$yyyymmdd} = "$1/$2/$3";
$ordered{$yyyymmdd} = "$3/$1/$2";

}
}

# all data loaded, sort array, split key into sort helper and text, and print

for (sort keys %ordered) {
$text = substr($_,9);
print qq($ordered{$_} $text\n);

}
pareshan
Regular Advisor

Re: How to just extract expiry date and server name from the following file

I know I have put just to extract expiry date and server name in the beginning because the extracted data was in bad format but now I am able to solve that part and I have data in fine format like I have posted above

astro 08/31/2004 08/31/2009 permanent Number of CPUs : 64
benji 08/16/2001 08/15/2011 permanent Number of CPUs : 64
bhpbob10 03/30/2006 03/29/2011 permanent Number of CPUs : 16
bhptlg82 02/24/2009 03/26/2009 temporary Number of CPUs : 16

Note:: And dont confuse::second one is the expiration date based on that I have to categorize in three levels just as hein did

already expired or will expire within a wekk

will expire between 8 to 30th day and

after 30th day

Nothing wrong with the above script just I am not able to match it with my real data which I have posted here

Thank YOu Very Much
U guys have gud weekend
waitting for the solution
Hein van den Heuvel
Honored Contributor

Re: How to just extract expiry date and server name from the following file

He's back! Hello again.

>> But I cant use with any other data than the sample I have provided before, it just gives me blank for other data.

Right, that is fairly typical of these programs. They need to 'latch on' to some piece of strinf and we tends to look for shortcuts, for easy recognible pattern in portions of the data which are of use.

For that JRF picked, and I continued with "a piece of string that looks like a date followed by a double quoted world:
Notably: m{(\d\d/\d\d/\d{4})\s+(".+?")}
In English that says: Match on and remember first ( 2 decimals, a slash, 2 mode decimals, a slash, 4 decimals) then any number of spaces and secondly remember if there is a double quote, a series of non-quotes, and a final quote.
Try to map that to appreciate it!

So now you seem to tell us that the data really looks like: a line beginning with a word date end-date and stuff.
We can match on that like so:
if ( m{^\s*(\S+)\s+[0-9/]+ (\d\d)/(\d\d)/(\d{4})\s} ) {

But now the 'remembered' chunks are slightly re-ordered so we also need to change to:
$yyyymmdd = $4.$2.$3.' '.$1;
$ordered{$yyyymmdd} = "$2/$3/$4";

That fixes your core problem.

>> And also it doesnt give me all the data from the file just it gives some columns. I need all the data from the file in output

Now he tells us! If you looks back in the original question you wrote: " I just have to extract expire date ... and the server name".
So that's what we did!

So now you also want to remember the 'stuff' following the 2nd date.... or... the whole line?

Oki doki, Let's just do the whole line!
Then we no longer need to 'find' the the node name. Much easier. Just look for a (tail-end-of-a) date, following a date. That match string _could_ be :
m{/\d+\s+(\d\d)/(\d\d)/(\d{4})\s}

Interstingly enough, the keen observer will notice that the above match will work on both the originally stated format as the real format! Try it :-).

Anyway, Now we no longer need and associative array no more either. Just a list will do. Just gotta sort it right!
It's all much easier.

Here is new code to try:

-------------------
print "\n--- The servers expired already or will expire within 7 days ---\n\n";

# load a 7 day cutoff marker

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 7 *86400); #
$ymd = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
push @lines, qq($ymd \n--- The servers will expire between 8 to 30th day ---\n\n);

# load a 30 day cutoff marker

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 30 *86400); #
$ymd = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
push @lines, qq($ymd \n--- these will nor expire for at least 30 days -----\n\n);


while (<>) {
if ( m{/\d+\s+(\d\d)/(\d\d)/(\d{4})\s} ) {
push @lines, qq($3$1$2 $_);
}
}

# all data loaded, sort array, split key into sort helper and text, and print

for (sort {substr($a,0,8) cmp substr($b,0,8)} @lines) {
print substr($_,9);
}
---------------------------------------

And new result for (slightly editted to show all categories) data in old and new shape

----------------------------------------

C:\temp>perl tmp.pl tmp.txt

--- The servers expired already or will expire within 7 days ---

# SyncSort UNIX 03/31/2003 09/15/2008 "dhpgpl2" "9000/800" 16 * * * V876-U-2
bhptlg82 02/24/2009 03/26/2009 temporary Number of CPUs : 16

--- The servers will expire between 8 to 30th day ---

# SyncSort UNIX 08/31/2004 03/31/2009 "dhpmafg1" "9000/800" 64 * * * X280-A-1

--- these will nor expire for at least 30 days -----

# SyncSort UNIX 08/31/2004 08/31/2009 "astro" "9000/800" 64 * * * X280-D-1
# SyncSort UNIX 08/31/2004 08/31/2009 "dhpalh2" "9000/800" 16 * * * X280-A-3
astro 08/31/2004 08/31/2009 permanent Number of CPUs : 64
bhpbob10 03/30/2006 03/29/2011 permanent Number of CPUs : 16
benji 08/16/2001 08/15/2011 permanent Number of CPUs : 64

--------------------------------------

Oops.. you long exceeded your 'first 15 minutes are free' quota.
Send money if you need more teaks from me!

Cheers,
Hein.







pareshan
Regular Advisor

Re: How to just extract expiry date and server name from the following file

when I tried , Got this error, couldnt find error, everything seems alright to me but..


./line.pl extracted_data.dat

--- The servers expired already or will expire within 7 days ---


./line.pl[3]: syntax error at line 7 : `=' unexpected
pareshan
Regular Advisor

Re: How to just extract expiry date and server name from the following file

While trying with the other one u said with the real data, it does gives output but not the way you said.
---------------------
the real data is

cat result
astro 08/31/2004 08/31/2009 permanent Number of CPUs : 64
benji 08/16/2001 08/15/2011 permanent Number of CPUs : 64
bhpbob10 03/30/2006 03/29/2011 permanent Number of CPUs : 16
bhptlg82 02/24/2009 03/26/2009 temporary Number of CPUs : 16

Note: Everything is in the same line just it doesnt fit here.
the output while executing the script, it still gives the same output, not the whole data. I have tried another one which one supposed to give whole line in the output but got some error.

./extract.pl result

The servers expired already or will expire within 7 days

03/26/2009 bhptlg82

The servers will expire between 8 to 30th day


The servers will expire after 30th day

08/31/2009 astro
03/29/2011 bhpbob10
08/15/2011 benji


______________________

#!/usr/local/dazel/bin/perl

print "\n The servers expired already or will expire within 7 days\n\n";
# load a 7 day cutoff marker

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 7 * 86400);
$ymd = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
$mdy = sprintf qq(%02d/%02d/%d), $mon+1, $mday, $year+1900;
$ordered{qq($ymd The servers will expire between 8 to 30th day \n)} = "\n";

# calculate a 30 day cutoff time

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 30 * 86400);
# $ymd_30 = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
$ymd = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;
$mdy = sprintf qq(%02d/%02d/%d), $mday, $mon+1, $year+1900;
# $ordered{qq($ymd ---- 30 day cut-off ----)} = $mdy;
$ordered{qq($ymd The servers will expire after 30th day \n)} ="\n";


while (<>) {

if ( m{^\s*(\S+)\s+[0-9/]+ (\d\d)/(\d\d)/(\d{4})\s} ) {
$yyyymmdd = $4.$2.$3.' '.$1;
$ordered{$yyyymmdd} = "$2/$3/$4";

}
}

# all data loaded, sort array, split key into sort helper and text, and print

for (sort keys %ordered) {
$text = substr($_,9);
print qq($ordered{$_} $text\n);

}

___________________________

Script is really good but just it dint give all the lines in the output but it do do the work of levelling just need to add is whole line with it. I am trying to make it work but everytime get some weird result.

Thanks alto hein
Ur the best

Could you tell me what should I have to add so that I can get the whole line in the output?
pareshan
Regular Advisor

Re: How to just extract expiry date and server name from the following file

Well, Now I have done that part and know how to match different data.

Thanks to Hein,
I really appreciate it