LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   date substitution problem (https://www.linuxquestions.org/questions/linux-newbie-8/date-substitution-problem-785803/)

visitnag 01-30-2010 06:58 AM

date substitution problem
 
My infile:

despatching date 00/00/0000
despatching date 00/00/0000
despatching date 00/00/0000
despatching date 00/00/0000

==========================================================
My code:

clear
echo -n " Enter Date as ddmmyyyy : "
read dat
echo $dat > xx
dd=`cut -c1-2 xx`
mm=`cut -c3-4 xx`
yy=`cut -c5-8 xx`
sed 's/00\/00\/0000/'$dd'\/'$mm'\/'$yy'/g' infile > outfile
rm xx

===========================================================
My Outfile:

despatching date 01/01/2010
despatching date 01/01/2010
despatching date 01/01/2010
despatching date 01/01/2010

Is there any shortcut for this especially in awk? Kindly guide me. Thank you.

kbp 01-30-2010 07:07 AM

Code:

#!/bin/bash

read -p " Enter Date as dd mm yyyy : " DAY MONTH YEAR
sed 's/00\/00\/0000/'$DAY'\/'$MONTH'\/'$YEAR'/g' infile > outfile

seems to work ok

cheers

visitnag 01-30-2010 09:12 AM

Yeah! It will work, but the user will get confuse when giving the sapce between date,month,year. I mean they generally give date as 12122010 instead of 12 12 2010. Now gimmi some idea how to specify the fixed place to the date fields i.e.,
as soon as the user enters DAY number it should jump into the MONTH field and then next to YEAR field. Screen display as following...


Enter date as dd mm yyyy : <DAY> / <MONTH> / <YEAR>


I did something different using awk, but it is also almost same like your solution....

clear
read -p " Enter Date as ddmmyyyy : " dat
awk '{
dd=substr('$dat',1,2);mm=substr('$dat',3,2);yy=substr('$dat',5,4);
sub(/00\/00\/0000/,dd"/"mm"/"yy,$0)
print $0
}' infile > outfile

but my request is there should be only
2 digit place for day
2 digit place for month
4 digit place for year


All times are GMT -5. The time now is 02:41 AM.