Replace Date in a fixed length file

Hello All,

I working on ksh. I am using fixed length file. My file is like:

IXTTIV110827 NANTH AM IKSHIT
ABCDEF  0617 IJAY NAND EENIG
ZXYWVU  0912 AP OOK OONG
PQRSTU100923  NASA  DISH TTY
ASDFG   0223  GHU UMA LAM
QWERT   0111  ATHE SH  THEW

=======

From 7th to 12 is a date field.
If date is supplied as yymmdd then I should leave the record as is. If the record is supplied as mmdd then I need find month from system date and compare againt month supplied in the file. If month from system date is greater than or equal to month supplied in file then I should add currect year to the date supplied in file else I add previous year to the date.

month from sysdate: 05
I am expecting Output like below:

IXTTIV110827 NANTH AM IKSHIT
ABCDEF120518    NAND EENIG  
ZXYWVU120912 AP OOK OON 
PQRSTU100923  NASA  DISH TTY
ASDFG 110223  GHU UMA LAM
QWERT 110111  ATHE SH  THEW

=======

I wrote code as below:

echo "Start"
d=$(date +%Y%m%d)
_yymmdd=${d#??}
_mmdd=${d#????}
_mm=$(date +%m)
_yy=`echo ${_yymmdd}|cut -c1-2`
_yy1=`expr ${_yy} - 1`

cut -c7-12 test.dat |
while read _dd ; do
 ln=`echo ${#_dd}`
 if [[ ${ln} -eq 4 ]] ;
 then
   _fmm=`echo ${_dd} | cut -c1-2`

 if [[ ${_mm} -ge ${_fmm} ]] ; then
      sed 's/  ${_dd}/${_yy}${_dd}/g' test.dat >> test_output.dat
     else
    sed "s/  ${_dd}/${_yy1}${_dd}/g" test.dat >> test_output.dat
   fi
  else
    echo false
 fi
done
echo "End"

====
But it is not working properly.
Please help me.

execute the below command and let me know..

 
m=$(date +%m);cy=$(date +%y);py=$(echo $(date +%y) - 1 | bc);awk -v m="$m" -v py="$py" -v cy="$cy" '{firstpart=substr($0,1,6);lastpart=substr($0,14,length($0));a=substr($0,7,6);gsub(" ","",a);if(length(a)<6){if(substr(a,1,2)<m){a=py""a;print firstpart""a""lastpart}else{a=cy""a;print firstpart""a""lastpart}}else{print}}' input.txt
1 Like

Welcome to the forum. Please view this link to learn the use of code tags.

[root@host dir]# cat input
IXTTIV110827 NANTH AM IKSHIT
ABCDEF  0617 IJAY NAND EENIG
ZXYWVU  0912 AP OOK OONG
PQRSTU100923 NASA DISH TTY
ASDFG   0223 GHU UMA LAM
QWERT   0111 ATHE SH THEW
[root@host dir]# 
[root@host dir]# cat test.sh
#! /bin/bash
while read x
do
    if [ "${x:6:2}" == "  " ]
    then
        if [ ${x:8:2} -ge $(date +%m) ]
        then
            echo "${x:0:6}$(date +%y)${x:8}"
        else
            echo "${x:0:6}$(date -d "-1 year" +%y)${x:8}"
        fi
    else
        echo $x
    fi
done < input
[root@host dir]#
[root@host dir]# ./test.sh
IXTTIV110827 NANTH AM IKSHIT
ABCDEF120617 IJAY NAND EENIG
ZXYWVU120912 AP OOK OONG
PQRSTU100923 NASA DISH TTY
ASDFG 110223 GHU UMA LAM
QWERT 110111 ATHE SH THEW
[root@host dir]#

Thank you so much. It worked. :slight_smile:

---------- Post updated at 07:27 PM ---------- Previous update was at 07:03 PM ----------

Dear Kamraj,
Your code is working perfectly for date but the length of file is being changed :(. The length of file should be same as previous.

---------- Post updated at 07:28 PM ---------- Previous update was at 07:27 PM ----------

Dear Balajesuri,
I am unable to execute your code in ksh. its returning me error saying "$ chmod 700 test.shl
$ ./test.shl
./test.shl[3]: "${x:6:2}": bad substitution".

Since OP originally asked for ksh solution ....

#!/bin/ksh93

month=$(printf "%(%m)T" "now")
thisyear=$(printf "%(%y)T" "now")
lastyear=$(printf "%(%y)T" "last year")

while read x
do
    if [[ "${x:6:2}" == "  " ]]
    then
        if [[ ${x:8:2} -ge $month ]]
        then
            echo "${x:0:6}${thisyear}${x:8}"
        else
            echo "${x:0:6}${lastyear}${x:8}"
        fi
    else
        echo $x
    fi
done < infile