Hello Experts,
I have a sample comma separated file as having date data in field # 5 and field #8. Field # 4 and 7 (fields before the date field) are indicators:
This is just a sample data, the actual file may have n number of date fields.
29,A Store,A Street,1,111213,aaaa,0,891213
30,B Store,B Street,0,991213,aaaa,1,61213
31,C Store,C Street,1,51213,aaaaa,1,81213
32,D Store,D Street,0,0,aaaa,1,150323
33,E Store,E Street,1,121212,bbbb,0,0
34,F Store,F Street,1,101212,cccc,0,971212
I need to update field #5 (depending on the value in field # 4 (indicator field)) and field #8 (depending on the value in field#7) and so on as :
-
If field # 4 = 0 and field # 5 <> 0 (say data is : 991213), then append '19' to the string in field # 5 and make the final value
as : 1999-12-13. This is for years before 2000 -
If field # 4 = 0 and field # 5 = 0 (say data is : 0), then the final value as : 0001-01-01
-
If field # 4 = 1, then
a. check if field # 5 has length = 5, then (for data say: 51213), append '200' so that the final value should be 2011-12-13
b. Else, (for data say : 111213), append '20' so that the final value appears as : 2011-12-13.
Input parameters to the script :
$1 : file name
$2 , $3, ........ this depends on the number of date fields which need to be transformed.
I have written the following code:
#! /usr/bin/ksh
# Read File name
INPUT=$1
while IFS=',' read -r f1 f2 f3 f4 f5 f6 f7 f8
do
For Century = 0
if [ $f4 -eq 0 -a $f5 -ne 0 ]
then
echo "19$f5" | sed 's/./&-/4;s/./&-/7' ;
fi
For 0 in date
if [ $f4 -eq 0 -a $f5 -ne 0 ]
then
echo "00010101" | sed 's/./&-/4;s/./&-/7' ;
fi
for century = 1
if [ $f4 -eq 1 ]
then
char_len=`expr length $f5`
if [ $char_len -eq 5 ]
then
echo "200$f5" | sed 's/./&-/4;s/./&-/7';
else
echo "20$f5" | sed 's/./&-/4;s/./&-/7';
fi
fi
done < "$INPUT"
I am able to transform the data for Field # 5 only.
Could you please suggest a better approach for this requirement such that I am able to transform the data for field # 8 as well.
Thank you