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!

Substitute date for another date format

Status
Not open for further replies.

alqsp

Programmer
Dec 12, 2001
9
AU
Hi,

I have a file with date's in this format 30-JAN-00 that I need to convert to this format 2000-01-3000.00.00.

Any ideas on how to first search the file line by line for a month (jan-dec) and then confirm it's a date field (DD-MMM-YY) because it's possible the text JAN, FEB etc. can appear in a comments field on the file.

Cheers.
 
gnu's version of date will take your input with -d and output it however you would like.
 
# Here's my take:
# don't even want to do anything if there's no dashes.
# assume data 30-JAN-00 like this is a field all by itself
# split the field into 3 if it has dashes
# change month to a number
# if all 3 fields are numeric change the field to new structure

# I leave the possible Y2K problem to you, and I leave it
# to you to add your time string.

#!/bin/ksh


while read line
do
if ( echo $line|grep - > /dev/null )
then
line=$(echo $line|nawk -f awkfile)
fi
echo $line >> new.file

done < data.file

# and here's awkfile:

# awkfile
{
# for each field
for(i=1; i<=NF; i++)
{
# need a dash
if ( match($i, &quot;-&quot;) > 0)
{
# 3 fields
anf=split($i, a, &quot;-&quot;)
if (anf == 3)
{
mno=month_num(a[2])
# set your field if it's numeric
if ( is_numeric(a[1]) && is_numeric(mno) && is_numeric(a[3]) )
$i=sprintf(&quot;20%s-%02d-%02d\n&quot;, a[3], mno, a[1])
}
}
}
print $0
}

function is_numeric(field) {
if ( field ~ /^[0-9]+$/ )
return 1
else
return 0
}

function month_num(month) {

if (month == &quot;JAN&quot;)
return 1
if (month == &quot;FEB&quot;)
return 2
if (month == &quot;MAR&quot;)
return 3
if (month == &quot;APR&quot;)
return 4
if (month == &quot;MAY&quot;)
return 5
if (month == &quot;JUN&quot;)
return 6
if (month == &quot;JUL&quot;)
return 7
if (month == &quot;AUG&quot;)
return 8
if (month == &quot;SEP&quot;)
return 9
if (month == &quot;OCT&quot;)
return 10
if (month == &quot;NOV&quot;)
return 11
if (month == &quot;DEC&quot;)
return 12
return &quot;E&quot;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top