Change File Header Timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change File Header Timestamp
# 1  
Old 12-07-2008
Change File Header Timestamp

Please I am new to Unix and this simple question I am already answered but struggling to find the answer.

I have a data file which contains header record which conatins date timestamp. I need to find a way of simply updating the date time stamp to current date timestamp.

So if the header record is:

20081206 then I simply want to update it to 20081207 (i.e. today's date).

I would appreciate if someone can either point me to the related link or simply answer the this question by providing simple code. Note I would add this code to an existing shell script.

Thanks.
# 2  
Old 12-07-2008
Hi,

Code:
sed "s/200[012345678][01][0-9][0123][0-9]/$(date +%Y%m%d)/" file >> file.new

will match all the days since "20000000" and replace them by the current
date.

HTH Chris
# 3  
Old 12-07-2008
Sorry I forgot to mention the date in the header record is in dd-mmm-yyyy format e.g. 12-Sep-2008 and there are data records which contain date in the format dd-mm-yyyy e.g. 21-07-2005. So I need to change the date in the header record only.

from:
SDM|PRDHIER|12-Sep-2008|12:00||
CDBCIN|1012348318|C5C18GB|CIS|CIN|21-07-2005
CDBCIN|1012348381|Z3WA3GB|CIS|CIN|31-12-2007
CDBCIN|1012348407|CBFTVGB|CIS|CIN|26-02-2007
CDBCIN|1012348416|CZQO4GB|CIS|CIN|30-03-2006

to:
SDM|PRDHIER|07-Dec-2008|12:00||
CDBCIN|1012348318|C5C18GB|CIS|CIN|21-07-2005
CDBCIN|1012348381|Z3WA3GB|CIS|CIN|31-12-2007
CDBCIN|1012348407|CBFTVGB|CIS|CIN|26-02-2007
CDBCIN|1012348416|CZQO4GB|CIS|CIN|30-03-2006

i.e. only the header record and the rest remains the same.

Thanks.
# 4  
Old 12-07-2008
It's all the same. All you need is:

Code:
man date

and the snippet i gave you becomes:

Code:
sed "s/[0123][0-9]-[A-Z][a-z]\{2\}-200[0-9]/$(date +%d-%b-%Y)/" file

# 5  
Old 12-07-2008
Thanks for this, it works perfectly...
# 6  
Old 12-08-2008
Code:
sed 's/[0-9][0-9]-[a-zA-Z][a-zA-Z][a-zA-Z]-[0-9][0-9][0-9][0-9]/'`date +%d-%b-%Y`'/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

writing the timestamp to as a header in a file

hello mates, this is my first post. please help me out. i have got a file with some data in it. i am asked to write the timestamp as a header for that file. i mean the time the file created should be mentioned at the top of the file. i know we can use sed to insert a sentence but... (6 Replies)
Discussion started by: jdsony
6 Replies

3. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

4. Shell Programming and Scripting

Getting a relative timestamp from timestamp stored in a file

Hi, I've a file in the following format 1999-APR-8 17:31:06 1500 3 45 1999-APR-8 17:31:15 1500 3 45 1999-APR-8 17:31:25 1500 3 45 1999-APR-8 17:31:30 1500 3 45 1999-APR-8 17:31:55 1500 3 45 1999-APR-8 17:32:06 1500 3 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

5. Shell Programming and Scripting

Trying to change header date of a file

Hi, I am trying to write a code for changing header date of a file with a date of 6 month back. Below is the header date on which I am working - 20080531 I want to change it as 20071130 Please suggest me in the way like ,I can change any date in a file with date of 6 months back. ... (9 Replies)
Discussion started by: Monalisa
9 Replies

6. AIX

Change a file's timestamp

Hi, I want to modify a file and then change the file's timestamp back to what it was before the modification. It should look as if the file has'nt been modifed. The entire operation needs to be done inside a ksh script. Can anyone suggest how I can trap a file's stamp in a variable within a... (6 Replies)
Discussion started by: shibajighosh
6 Replies

7. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies

8. UNIX for Dummies Questions & Answers

move file change timestamp

Hello, Alright solution: I need to move files to a backup folder, changing the datestamp to the current day so that the file stays in the backup folder for the full 30 days before another script removes it. Obviously, any file I move in will preserve the timestamp which is what I *don't* want.... (2 Replies)
Discussion started by: tekster757
2 Replies

9. HP-UX

change timestamp of file

Hi, How I change the timestamp of file to some past date Let's say I have this file -rw-r--r-- 1 oracle dba 16587 Apr 11 10:46 create_dev.sql And I want to change the timestamp to Mar 10th 7:45 PM So it should appear like this: -rw-r--r-- 1 oracle dba ... (1 Reply)
Discussion started by: isingh786
1 Replies

10. UNIX for Advanced & Expert Users

Timestamp of File permission change

Hi!! Experts, Is there any way to find the timestamp when the permission of a file was modified?? I mean no change to file contents.. Just the chnage of permissions. :) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question