Replace Date in a fixed length file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Date in a fixed length file
# 1  
Old 05-02-2012
Replace Date in a fixed length file

Hello All,

I working on ksh. I am using fixed length file. My file is like:
========
Code:
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:
========
Code:
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:
======
Code:
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.

Last edited by Scrutinizer; 05-02-2012 at 06:52 AM.. Reason: Code tags
# 2  
Old 05-02-2012
execute the below command and let me know..

Code:
 
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

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 05-02-2012
Welcome to the forum. Please view this link to learn the use of code tags.

Code:
[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]#

# 4  
Old 05-02-2012
Thank you so much. It worked. Smilie

---------- 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 Smilie. 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".
# 5  
Old 05-02-2012
Since OP originally asked for ksh solution ....
Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace and Increment a value in the fixed length file

Hi Everyone, I need to increment a value in the fixed length file. The file has almost a million rows. Is there any easy way to accomplish this. Ex input file ASDSD ADSD 00000 X AAASD ADSD 00000 X SDDDD ADSD 00000 X Ouput ASDSD ADSD 00001 X AAASD ADSD 00002 X SDDDD ADSD 00003 X ... (7 Replies)
Discussion started by: saratha14
7 Replies

2. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

3. Shell Programming and Scripting

Remove characters from fixed length file

Hello I've question on the requirement I am working on. We are getting a fixed length file with "33" characters long. We are processing that file loading into DB. Now some times we are getting a file with "35" characters long. In this case I have to remove two characters (in 22,23... (14 Replies)
Discussion started by: manasvi24
14 Replies

4. Shell Programming and Scripting

Search and replace particular characters in fixed length-file

Masters, I have fixed length input file like FHEAD0000000001XXXX20090901 0000009000Y1000XXX2 THEAD000000000220090901 ITM0000109393813 430143504352N22SP 000000000000RN000000010000EA P0000000000000014390020090901 TTAIL0000000003000000 FTAIL00000000040000000002 Note... (4 Replies)
Discussion started by: bittoo
4 Replies

5. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

6. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

7. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

8. Shell Programming and Scripting

sed replace with fixed length

$ cat template s.noName $ sed "s/s.no/1/" template > out $ sed "s/s.no/100/" template >>out $ cat out 1Name 100Name 1 Name 100Name (7 Replies)
Discussion started by: McLan
7 Replies

9. Shell Programming and Scripting

Manipulating a fixed length file w/o PERL

Greetings, I need to take a fixed length file, similar to the following: <input file> 1233 e 612 i 43378 f 03 x 22 17 e 9899 a 323e a6 z7 read in the character in position 6, and if that character = e, delete that line from the file. <output file> 43378 f 03 x 22 17 e 9899 ... (4 Replies)
Discussion started by: dabear
4 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question