I have a file test.txt
with multiple records as below.
100,200,300,08-May-2012 11:24:25
100,400,300,25-May-2012 09:24:25
Now I want to output the data using the following "format":
$1,$2,$3,$4,$4+30days,$4+60days,$4+90days
How can I do that using awk
?
script.awk
file
function map_month(m)
{
if (m ~ /[jJ][aA][nN]/) return 1;
if (m ~ /[fF][eE][bB]/) return 2;
if (m ~ /[mM][aA][rR]/) return 3;
if (m ~ /[aA][pP][rR]/) return 4;
if (m ~ /[mM][aA][yY]/) return 5;
if (m ~ /[jJ][uU][nN]/) return 6;
if (m ~ /[jJ][uU][lL]/) return 7;
if (m ~ /[aA][uU][gG]/) return 8;
if (m ~ /[sS][eE][pP]/) return 9;
if (m ~ /[oO][cC][tT]/) return 10;
if (m ~ /[nN][oO][vV]/) return 11;
if (m ~ /[dD][eE][cC]/) return 12;
return 0;
}
function cvt_timestamp(str, t, a)
{
split(str, a, /[- :]/)
a[2] = map_month(a[2])
#print a[3] " " a[2] " " a[1] " " a[4] " " a[5] " " a[6]
t = mktime(a[3] " " a[2] " " a[1] " " a[4] " " a[5] " " a[6])
#print t
return t
}
function fmt_timestamp(t)
{
return strftime("%d-%b-%Y %H:%M:%S", t)
}
BEGIN { FS = OFS = "," }
{
tm = cvt_timestamp($4);
t0 = fmt_timestamp(tm + 0 * 86400)
t1 = fmt_timestamp(tm + 30 * 86400)
t2 = fmt_timestamp(tm + 60 * 86400)
t3 = fmt_timestamp(tm + 90 * 86400)
print $1, $2, $3, t0, t1, t2, t3
}
There's probably a better way to do the month abbreviation to number mapping; have at it. Using functions in awk
is valuable just the same as it is in any other language with functions.
Example data
file
100,200,300,08-May-2012 11:24:25
100,400,300,25-May-2012 09:24:25
100,400,300,15-Sep-2012 09:24:25
100,400,300,29-Feb-2012 09:24:25
Example output
$ gawk -f script.awk data
100,200,300,08-May-2012 11:24:25,07-Jun-2012 11:24:25,07-Jul-2012 11:24:25,06-Aug-2012 11:24:25
100,400,300,25-May-2012 09:24:25,24-Jun-2012 09:24:25,24-Jul-2012 09:24:25,23-Aug-2012 09:24:25
100,400,300,15-Sep-2012 09:24:25,15-Oct-2012 09:24:25,14-Nov-2012 08:24:25,14-Dec-2012 08:24:25
100,400,300,29-Feb-2012 09:24:25,30-Mar-2012 10:24:25,29-Apr-2012 10:24:25,29-May-2012 10:24:25
$
If you decide you want to handle switches in time zone differently, that's your prerogative; you can fix the code.