Replace date in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace date in a file
# 1  
Old 06-24-2014
Hammer & Screwdriver Replace date in a file

Hi,

I have a file named as 'Todays.prm'. This file has a date on a second line. I want to replace the date to todays date whenever this command is executed.

My File looks as below:

Code:
[GLOBAL]
$$TODAYSDATE=2014-06-24-18.45.50

When this command will be run tomorrow the data in the file shoudl look like

Code:
[GLOBAL]
$$TODAYSDATE=2014-06-25-18.45.50

The variable $$TODAYSDATE will always be on second line of the file. Could you please help me out with this. I tried using SED/AWK coomand but nothing seemed to be working.

-Vrushank Patel

Last edited by Scrutinizer; 06-24-2014 at 04:52 AM.. Reason: code tags
# 2  
Old 06-24-2014
Welcome to forums,

Try [untested]

Code:
$ sed "2s/=.*/=$(date +"%Y-%m-%d-%H.%M.%S" )/" file

Code:
$ awk 'FNR==2{sub(/=.*/,strftime("=%Y-%m-%d-%H.%M.%S",systime()))}1' file

# 3  
Old 06-24-2014
Thanks Akshay. When I run this cmd it displays me the content of file with updated date but when I try to open up File its still showing the old date.Its not updating the file. Is this because the owner of the file is someone else and I am trying to 'sed' it with different ID?

-Vrushank Patel
# 4  
Old 06-24-2014
use this sed -i.bak "2s/=.*/=$(date +"%Y-%m-%d-%H.%M.%S" )/" file
Code:
-i[SUFFIX], --in-place[=SUFFIX]
   edit files in place (makes backup if extension supplied)

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 06-24-2014
The common way is to redirect the output output to a new file.
some_cmd file > newfile

If the output is correct then you can replace the old file with the new file. If that file is important, you can chose to make an extra backup of the original file first:

Code:
mv file file.backup
mv newfile file

You can also do something like this:
Code:
some_cmd file > newfile && mv newfile file

That way the the mv operation is only performed if the first part was technically successful and thus the part after && gets executed..

Last edited by Scrutinizer; 06-26-2014 at 06:22 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 06-26-2014
Thank you Akshay & Scrutinizer. It worked for me.
# 7  
Old 06-26-2014
You specified to change the date only. Akshay Hegde's proposal will replace the time as well. To just replace the date part, try
Code:
sed "2s/=.*-.*-.*-/=$(date +"%Y-%m-%d" )-/" file

 
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. 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

5. 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

6. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: jakSun8
9 Replies

7. Shell Programming and Scripting

Searching for unknown date inside the file and replace to new date

Hello, Iam a newbies to Shell scripting. Iam trying to replace the date inside the file to new date. is there anyway that we can just use the pattern to search as "..." I have many files want to replace with the same date, and each file contains different date. Thanks for your help. ... (2 Replies)
Discussion started by: Daro
2 Replies

8. Shell Programming and Scripting

Replace Date in a fixed length file

Hello All, I working on ksh. I am using fixed length file. My file is like: ======== 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... (4 Replies)
Discussion started by: AnanthaDikshit
4 Replies

9. Shell Programming and Scripting

Replace Date field in Unix File

I have a data file having first 19 characters having the date in the below format- 2010-04-29-00.00.00 I have to check this date field ( first 19 characters) against some specific dates and if date is not in 3 valid dates ( business date available to me , business date - 1 , businessdate... (10 Replies)
Discussion started by: varunrbs
10 Replies

10. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies
Login or Register to Ask a Question