Replace date on a line with current date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace date on a line with current date
# 1  
Old 10-14-2012
Replace date on a line with current date

Hi Guys,
I have a file with following content

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

Code:
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
# 2  
Old 10-14-2012
Assuming, the line always be the first line..

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

# 3  
Old 10-14-2012
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
# 4  
Old 10-15-2012
Do you have GNU date?
# 5  
Old 10-15-2012
Yes. I tried this on my shell

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

# 6  
Old 10-15-2012
Try...
Code:
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


Last edited by Ygor; 10-15-2012 at 11:54 AM..
# 7  
Old 10-15-2012
Hi Ygor,

I tried and am getting this error when i run

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

thanks - jak
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

3. UNIX for Beginners Questions & Answers

“sed” replace date in text file with current date

We want to call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ Code: line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to write a... (3 Replies)
Discussion started by: pradeepp
3 Replies

4. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

5. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

6. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

7. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

8. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

9. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

10. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies
Login or Register to Ask a Question