Search for a string and replace the searched string in the same position

Hi All,

My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last two digits in "0108" tells the year..The file may contain "0109","0110" and so on...i need to replace the correct year depending on the last two digits of the string..How do i go about??please help me....

eg

00000000 0 +00000000.00 2008/12/29 11:06:04 0108/12/29 10:05:37 00521363

i need to change this to

00000000 0 +00000000.00 2008/12/29 11:06:04 2008/12/29 10:05:37 00521363

Thanks in advance!!!

use this...

awk '{sub (/0108/,"2008"); print }' infile

$ sed -e 's/0108/2008/' infile > outfile

This is not working whiteboard.....it jus replaces the string and displays since print statement is given in AWK.... i need to replace in the actual file itself.....

Redirect it to a new file..

awk '{sub (/0108/,"2008"); print }' infile > outfile

The file contains many numerical values ...so there may be many places where 0108 occurs...i need to find the string 0108/ and search it with 2008...how to do it white???

$ sed -e 's|0108/|2008/|' infile > outfile

Use global substitution...

awk '{gsub (/0108/,"2008"); print }' infile > outfile

white its working fine when i hardcode the values 0108/ with 2008..but i do not know which year will come in the wrong format...it may be 0109 or 0199 or anything...i need to search like for the 0***/ pattern i.e the year get the last two char and depending on it i need to replace it with the corresponding year...
for eg i get 0109/ i need to replace with 2009...
in the next line i may get 0110/ i need to replace with 2010...

so how to go about it ??

ok i can do like this for 0108/
but if i need to replace for 0109/ , 0110/.....0199/ ????

i need to put this in a loop and check the last two digits of the 0***/ and replace it with the corresponding year...
My question is can i use"$" variables inside awk'.....' ???? like awk '{gsub (/0$i/,"20$j")....' ????

If your file has a always the same format you can try this command:

$ echo '00000000 0 +00000000.00 2008/12/29 11:06:04 0108/12/29 10:05:37 00521363'| awk '{sub("01", "20", $6)}'1
00000000 0 +00000000.00 2008/12/29 11:06:04 2008/12/29 10:05:37 00521363

Regards

In this awk '{sub("01", "20", $6)}'1 ...what is that 1 stand for ???

awk evaluates the 1 as true and the prints the entire line by default.

Regards

Thanks a Lot Franklin!!! It worked....:slight_smile:

Thanks for ur support and help Whiteboard...
Its working fine with the help i got from Franklin and Zaxxon...!!!

Thanks alot guys!!!Am grateful to you al :wink:

sed -i 's|0108/|2008/|' infile
the original file will be over written