Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what script is best for this? 1

Status
Not open for further replies.

ixnay5

MIS
Jan 10, 2002
68
US
hey all,

i have a need for a script that can read a text file containing info such as:

1esm
P
20030224
20030228
0
1
1
1
0
1
0
P
0

find the "date" fields in the file (rows 3 and 4), analyze them, do something with them (say, add 7 days to each), write the new values into the file, and then close. the result of the script (adding 7 days to each date) would be the following:

1esm
P
20030303
20030307
0
1
1
1
0
1
0
P
0

i know zip.squat about unix scripting, except that there are a number of scripting languages available, any of which i'm willing to learn. what's the best choice for this type of problem?

much appreciated,
ixnay5
 
I would recommend perl for this situation because you will not only need to match the pattern for the date string, but perform date math. Perl has many functions that can do this. Here is an example that will store the year, month, and day into separate variables that you can then use a date function or two on:

#! /bin/perl
$infile = "date.file";
open(FILE, "$infile") || die "error opening $infile $!";
while (<FILE>) {
if (m/^(20\d{2})(\d{2})(\d{2})$/) {
$year = $1; $month = $2; $day = $3;
print &quot;$year $month $day\n&quot;;
# Manipulate variables here
}
}
close FILE;

I'm sure you can find someone to do the date math part of this problem if you repost to the perl forum.

-jim
 
Ok, i found the module you need. It's Date::calc. Here is an example that might work for you (i don't have this module on my system - you may need to get it from CPAN.org also):

#! /bin/perl
use Date::Calc qw( Add_Delta_Days);
$Dd = 7; # Days to add

$infile = &quot;date.file&quot;;
open(FILE, &quot;$infile&quot;) || die &quot;error opening $infile $!&quot;;
while (<FILE>) {
if (m/^(20\d{2})(\d{2})(\d{2})$/) {
$year = $1; $month = $2; $day = $3;
($year,$month,$day) = Add_Delta_Days($year,$month,$day, $Dd);
print &quot;${year}${month}${day}\n&quot;;
} else {print;}
}
close FILE;

Here is more info on the module:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top