Replace date on a line with current date

Hi Guys,
I have a file with following content

From 20121014 : To 20121014
Number of days : 1 
1234
1245
1246
1111

Everyday i run my script i want to modify "To" date on the first line with current date. I have set the current date in script as

RUN_DATE=`date -u +%Y%m%d`

So i want to replace "To" date with $RUN_DATE. The "From" date never changes.
Also, i want to replace on second line (toDate-fromDate)+1. Right now it is 1. So when it runs tomorrow it should be 2.
Thanks - jak

Assuming, the line always be the first line..

awk 'NR==1 {$NF=date}1' date=$RUN_DATE file

Thanks clx - yes it will always be on 1st line. Also, how would i modify the second line with the difference between two days(numbers) + 1

Thanks for your help! _ jak

---------- Post updated at 02:47 PM ---------- Previous update was at 11:53 AM ----------

Correction on previous post. I need difference of two dates and not numbers update on line 2.

Thanks - jak

Do you have GNU date?

Yes. I tried this on my shell

date +"%Y%m%d" -d sunday 
20121021

Try...

awk ' NR == 1 {
              from = $2
              "date -u +%Y%m%d" | getline to
              $NF = to
      }
      NR == 2 {
              "expr $(date -d " to " +%s) - $(date -d " from " +%s)" | getline diff
              $NF = int(diff / 86400) + 1
      }
      {
              print $0
      }
      ' file1 > file2

Hi Ygor,

I tried and am getting this error when i run

sh: -c: line 0: syntax error near unexpected token `)'
sh: -c: line 0: ` +%s)'

thanks - jak

Use perl for date time calculations as the math would be incorrect when you run into the end of the month or year...

Works fine for me. What system are you on? Do you have nawk or gawk?

Hi Ygor,

I have awk and gawk.

uname -a
Linux 2.6.18-194.11.4.el5 #1 SMP Fri Sep 17 04:57:05 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
 whence awk
/usr/bin/awk

cat tmp.sh
#!/bin/ksh

STAT_FILE=myStats.txt
STAT_TMP_FILE=myStats.txt.tmp
 
awk ' NR == 1 {
              from = $2
              "date -u +%Y%m%d" | getline to
              $NF = to
      }
      NR == 2 {
              "expr $(date -d " to " +%s) - $(date -d " from " +%s)" | getline diff
              $NF = int(diff / 86400) + 1
      }
      {
              print $0
      }
      ' $STAT_TMP_FILE > $STAT_FILE

When i run

tmp.sh 
sh: -c: line 0: syntax error near unexpected token `)'
sh: -c: line 0: ` +%s)'

thanks - jak

---------- Post updated at 10:32 PM ---------- Previous update was at 02:01 PM ----------

I tried this on command line and it works fine

expr $(date -d " 20121016 " +%s) - $(date -d " 20121014 " +%s)

but inside the script i am getting the error

sh: -c: line 0: syntax error near unexpected token `)'
sh: -c: line 0: ` +%s) '

This line seems culprit

"expr $(date -d " to " +%s) - $(date -d " from " +%s) " | getline diff

Any other alternative to this particular line?
Thanks for your time and help - jak