Operating System - HP-UX
1825601 Members
2772 Online
109682 Solutions
New Discussion

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

 
SOLVED
Go to solution
pareshan
Regular Advisor

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

Hi Guys,

I am trying to write one script where I have to go to lots of boxes and read one text file which will have data like this though this is not complete data. And I just have to extract expire date ( which is 2nd date value 08/31/2009 in the first one ) and the server name ( astro in first one ) which can be blank sometimes like "" from the text file and put it into another file for record

THis is the data inside that text file

# The following line is REQUIRED and must be the first line in license key file.
"Yours Trusted"
#
# SyncSort UNIX 08/31/2004 08/31/2009 "astro" "9000/800" 64 * * * X280-D-1
m1zy x530 4e13 hiej 1mr4 v3kt u833 8282
#
# SyncSort UNIX 08/31/2004 08/31/2009 "dhpalh2" "9000/800" 16 * * * X280-A-3
sny5 599a fvvc 4zdc c51y u0s9 fh32 7581
#
# SyncSort UNIX 09/15/2003 09/15/2008 "dhpgpl2" "9000/800" 16 * * * V876-U-2
px81 ivvi w8k1 m8hr 92h6 ynt8 um35 4279
#
# SyncSort UNIX 08/31/2004 08/31/2009 "dhpmafg1" "9000/800" 64 * * * X280-A-1
b5v9 cswp 9kzk v70s fc8t 9jwi 2835 3730


Any Help plese how to do it
I will appreciate the help
thank you
15 REPLIES 15
James R. Ferguson
Acclaimed Contributor

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

Hi:

One way:

# perl -nle 'm{(\d\d/\d\d/\d{4})\s+(".+?")} and print join " ",$1,$2' file
08/31/2009 "astro"
08/31/2009 "dhpalh2"
09/15/2008 "dhpgpl2"
08/31/2009 "dhpmafg1"

Regards!

...JRF...
pareshan
Regular Advisor

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

Thanks James your approach is right oo but I have done it using Unix and now I got list like this

01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/02/2007 "thfires"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
01/03/2011 "dhttrn2"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/10/2011 "dhpcrpt"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
02/28/2007 "wally"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr1"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/01/2010 "dhqglr2"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpbos1"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpdls2b"
03/14/2011 "dhpglr2b"
03/14/2011 "dhpglr2b"


now I have to sort the list in such a way that if expiration date has passed or within seven days from the date runs the script they should come first in the list followed by those between 8th day to 30th and rest whatever will be left and at the last number the list

Thank you
any help on this I think it should be easier I am trying also lets see what I can do

I will appreciate help
Hein van den Heuvel
Honored Contributor
Solution

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

Well, take the core data matching that JRF described, but don't print the result but stick it into an associative array.
Also add the two cut-off values in the array.
Sort the arry by keys and report! SMOP.

# perl test.pl test.txt
#
09/15/2008 "dhpgpl2"
23/03/2009 ---- 7 day cut-off ----
15/04/2009 ---- 30 day cut-off ----
08/31/2009 "astro"
08/31/2009 "dhpalh2"
08/31/2009 "dhpmafg1"
#
# cat test.pl

# 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), $mday, $mon+1, $year+1900;
$ordered{qq($ymd ---- 7 day cut-off ----)} = $mdy;

# 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;
$mdy = sprintf qq(%02d/%02d/%d), $mday, $mon+1, $year+1900;
$ordered{qq($ymd ---- 30 day cut-off ----)} = $mdy;

# load data record, with re-ordered date key

while (<>) {
if ( m{(\d\d)/(\d\d)/(\d{4})\s+(".+?")} ) {
$yyyymmdd = $3.$2.$1.' '.$4;
$ordered{$yyyymmdd} = "$1/$2/$3";
}
}

# 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);
}


Enjoy! (I needed a break).
Hein.

Hein van den Heuvel
Honored Contributor

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

too quick...

Change those 2 assignment to $mdy to read:

$mdy = sprintf qq(%02d/%02d/%d), $mon+1, $mday, $year+1900;

Result for sample data will be:
09/15/2008 "dhpgpl2"
03/23/2009 ---- 7 day cut-off ----
04/15/2009 ---- 30 day cut-off ----
08/31/2009 "astro"
08/31/2009 "dhpalh2"
08/31/2009 "dhpmafg1"


Hein.

pareshan
Regular Advisor

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

I am sure Your script will work but I am not supposed to have that kind of list, the list i need is like. Also the script looks quite complex if i need to change anything it will be hard for me.

From the list I have given in the 2nd post I need to make like this

The servers expired already or will expire within 7 days
12/21/2005 "agatha"
12/21/2005 "agatha"
12/21/2005 "agatha"
12/21/2005 "agatha"
12/21/2005 "agatha"
12/21/2005 "arthur"
12/21/2005 "arthur"
12/21/2005 "arthur"
12/21/2005 "arthur"
12/21/2005 "arthur"
12/21/2005 "arthur"
12/21/2005 "arthur"
12/21/2005 "arthur"

The servers will expire between 8 to 30th day

again whatver comes in the list after calculating form the text file


Rest which will expire after thirty days

all the rest servers and expirationt date



As I have already generated the list with the required value my problem is I am not able to sort the date field from older to newer.

Thanks
Dennis Handly
Acclaimed Contributor

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

>I am not able to sort the date field from older to newer.

You would have to split the date field into three and sort on YYYY then MM then DD. Or use a date field that sorts properly: YYYYMMDD
Hein van den Heuvel
Honored Contributor

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

Dennis.. that is of course exactly what my script does, and it is even commented!

Pareshan... if you are afraid to handle power tools, than stick to the basics.
Generate the list and use an editor!

And uh... do check out the result of the script for real. I really think it matched your requirement very closely. Ok, you want a line before the first series, and you don't want to see the rest? Easy adaption. See below.

Alos check out the script for real! It may look tricky on first reading, but it is really straightforward.

Also below, as bonus, an alternative approach which filters, but does not sort. Similar result, maybe easier to read.

others, if you have a perverse sense of humor like myself, then check out the "personal quote' attached to my profile for the last few weeks :-)


---- perl script even closer to request ---


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;

while (<>) {
if ( m{(\d\d)/(\d\d)/(\d{4})\s+(".+?")} ) {
$yyyymmdd = $3.$1.$2.' '.$4;
next if $ymd_30 lt $yyyymmdd; # skip anything over 30 day mark
$ordered{$yyyymmdd} = "$1/$2/$3";
}
}

# 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);
}




------------ alternative, just making 2 sub lists -----
------------ also 'cleaned' up with strict defintions -----




use strict;
use warnings;

my (@this_month, @this_week);

# calculate a 7 and 30 day cutoff marker in YYYMMDD format.

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 7 * 86400);
my $ymd_7 = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 30 * 86400);
my $ymd_30 = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;


# read the list, and ignore, or store in the appropriate sub list as needed.

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

my $yyyymmdd = $3.$1.$2;
print "$yyyymmdd $ymd_7 $ymd_30\n";
next if $yyyymmdd gt $ymd_30;
my $print_line = qq($1/$2/$3 $4\n);
if ($yyyymmdd gt $ymd_7) {
push @this_month, $print_line;
} else {
push @this_week, $print_line;
}
}
}

# all interesting data is loaded, print the arrays

if (@this_week) { print "\n The servers expired already or will expire within 7 days\n\n" , @this_week }

if (@this_month) { print "\n The servers will expire between 8 to 30th day\n\n", @this_month }


pareshan
Regular Advisor

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

Its great, It did give me the result I wanted but the problem is looks like I dint get all the values from the list because I have a big list like 5-10 thousand and result consists of 6 hundred values only.

may be you think I dint include the more than 30 days in the list but I have done using the earlier one you have suggested me because thats the one I want. Three list

first one expired or will expire under 7 days and second one between 8th and 30 and rest here the script I have used
#!/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;
#next if $ymd_30 lt $yyyymmdd; # skip anything over 30 day mark
$ordered{$yyyymmdd} = "$1/$2/$3";
}
}
# 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);
}

Hein van den Heuvel
Honored Contributor

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

I guess I was read the description wrong.
This brings us back to the first script pretty much.

To include all lines, you are right in commenting out the line:
#next if $ymd_30 lt

But the line "# $ordered{qq($ymd ---- 30 ..." must stay.

That is only for output formatting, not filtering

Here is the output for just 4 samples:
----------------------

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

The servers expired already or will expire within 7 days

09/15/2008 "dhpgpl2"

The servers will expire between 8 to 30th day

04/01/2009 "dhpalh2"

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

08/31/2009 "astro"
08/31/2009 "dhpmafg1"


Perl scripts below. Be sure to check my 'lt' and 'le' style choices.

( I guess you want the output in 3 files next? SMOP! Change the marker lines from "\n" to a file name. In the output loop, print to a 'file handle', not the default STDOUT. Check for the value of the array element or the key. If it looks like a marker/filename, use it! .. Open a new file :-)

Hein


------------------ code using sorted flavor -

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";

# 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;
$mdy = sprintf qq(%02d/%02d/%d), $mday, $mon+1, $year+1900;
$ordered{qq($ymd ---- these will nor expire for at least 30 days -----\n)} = "\n";

while (<>) {
if ( m{(\d\d)/(\d\d)/(\d{4})\s+(".+?")} ) {
$yyyymmdd = $3.$1.$2.' '.$4;
$ordered{$yyyymmdd} = "$1/$2/$3";
}
}

# 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);
}

-------------- code with sublist ---

use strict;
use warnings;

my (@later, @this_month, @this_week);

# calculate a 7 and 30 day cutoff marker in YYYMMDD format.

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 7 * 86400); #
my $ymd_7 = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time() + 30 * 86400); #
my $ymd_30 = sprintf qq(%d%02d%02d), $year+1900, $mon+1, $mday;


# read the list, and ignore, or store in the appropriate sub list as needed.

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

my $yyyymmdd = $3.$1.$2;
my $print_line = qq($1/$2/$3 $4\n);

if ($yyyymmdd gt $ymd_30) { push @later, $print_line; next }

if ($yyyymmdd gt $ymd_7) { push @this_month, $print_line; next }

push @this_week, $print_line;
}
}

# all interesting data is loaded, print the arrays

if (@this_week) { print "\n The servers expired already or will expire within 7 days\n\n" , @this_week }

if (@this_month) { print "\n The servers will expire between + 8 to 30 day\n\n", @this_month }

if (@later) { print "\n The servers will expire much later\n\n", @later}






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