Operating System - HP-UX
1753918 Members
7173 Online
108810 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}