Trying to change header date of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to change header date of a file
# 1  
Old 04-23-2010
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.

Can someone please help me on this.

Many Thanks in advance..

Monalisa
# 2  
Old 04-23-2010
do u know perl scripting ?

if yes, you can do it very simply ..
# 3  
Old 04-23-2010
Thanks..

But I know shell scripting not perl..

Can you please help me to do this with shell scripting?
# 4  
Old 04-23-2010
you can do it in awk or possibly with parameter substitution- part of standard shell.
Please show us sample input and desired output, and what shell you have: bash, ksh, etc.
# 5  
Old 04-23-2010
Code:
D="20080531"
date -d "$D 6 month ago" +%Y%m%d

returns 20071201 which is correct.
# 6  
Old 04-23-2010
Perl One Liner For -6 Month Date Delta

Code:
perl -MDate::Manip -pi.bak -e 's/(\d{8})/UnixDate(DateCalc("-6 M", $1), "%Y%m%d")/eg' inputfile

That will subtract six months from all dates in "inputfile" which are in the eight-digit format you showed. remove the "g" at the end if you only want to do one. This code consider that an incoming date is an eight digit number, with no punctuation. If you expect to have no-punctuation, eight-digit numbers in the data, we can work on this some more.

Hope that Helps.
# 7  
Old 04-23-2010
Quote:
Originally Posted by frans
Code:
D="20080531"
date -d "$D 6 month ago" +%Y%m%d

returns 20071201 which is correct.
Don't work for me Smilie
Quote:
Code:
# D="20080531"
# date -d "$D 6 month ago" +%Y%m%d
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]

Your solution is not universally valid , look like a Linux date Smilie, please state your environment next time , same for OP.

This works for me
Code:
# uname -v
FreeBSD 7.2-RELEASE-p7 #0: Sat Apr 10 23:18:49 JST 2010

# date -v -6m  "+%A %B %d %Y"
Friday October 23 2009

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Change date format in a file.

Hi all, I have a file as below, i would like the change the format of the time from "11/7/2019 20:12" to "2019-07-11 20:12:00" in the last coloumn. any awk solution on this. Input: 2,0,695016,1961612,497212,5800804,0,0,161,33,7605,12226,23,10,66,0,0,34,11/7/2019 20:10... (4 Replies)
Discussion started by: Raghuram717
4 Replies

2. Shell Programming and Scripting

Insert date/time header at top of file

I'm trying to take mrt output and put it at the top of a file along with the date and time. I was able to do it at the bottom of the file with the following printf "********** $(date) **********\n\n" >> $OUTPUT_PATH/$HOSTNAME mtr -r -w -c 10 $HOSTADDRESS >> $OUTPUT_PATH/$HOSTNAME printf... (2 Replies)
Discussion started by: kramer65
2 Replies

3. Shell Programming and Scripting

Script to change name of a file with date

I have a file that contains todays date(for ex- test_08/30/2013) Now I want a script which will run and change the name of the file and put yesterday's date (for ex- test_08/29/2013) Please help. ---------- Post updated at 04:40 AM ---------- Previous update was at 04:31 AM ---------- I... (2 Replies)
Discussion started by: sv0081493
2 Replies

4. UNIX for Dummies Questions & Answers

Can we change the file created date?

Hi, I am creating a file in unix today. Is it possible to make the file created as 2 days older (or some past date)? P.S: i dont want to change the system date to older one and try.:rolleyes: Thanks, Pandeeswaran (6 Replies)
Discussion started by: pandeesh
6 Replies

5. Shell Programming and Scripting

Extract date from file header and prefix it to all lines

Hello All, I have a file in the following format. I want to extract the date(020090930, 020090929) in the string "STPAGE020090930" and "STPAGE020090929" and prefix it to all lines below them. The output must be put into a new file. STPAGE020090930 xyzz aalc... (3 Replies)
Discussion started by: john2022
3 Replies

6. Shell Programming and Scripting

How to change date format in file

Hello! I have a textfile that look like this: "83d1:46:2b";"20091008190000";"Rögle BK - Skellefteå";"Swedish" "d4c:46:21";"20091008190000";"Södertälje - Brynäs";"Swedish" "d4b:46:2";"20091008190000";"HV 71 - Färjestad";"Swedish" "838:46:b";"20091010160000";"Skellefteå - HV 71";"Swedish"... (2 Replies)
Discussion started by: condmaster
2 Replies

7. Shell Programming and Scripting

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

8. UNIX for Dummies Questions & Answers

Move A File With Same Date,don't Change The Desitination Dir Date

Assume, I created one file three years back and I like to move the file to some other directory with the old date (Creation date)? Is it possible? Explain? (1 Reply)
Discussion started by: jee.ku2
1 Replies

9. UNIX for Dummies Questions & Answers

how can i change the date of an existing file

Hi, Plz suggest me how can i change the date of a file. Suppose my file has been created in some date and i want to give it present date. How can i do this???? (2 Replies)
Discussion started by: adityam
2 Replies

10. Solaris

File Permissions change date

Hi, I was wondering if there is any way to find out when the file/directory's permissions were changed. Typically, when using 'chmod' to change a file/directory's permissions, the modification date does not change. Any help would be greatly appreciated. Thanks. (4 Replies)
Discussion started by: kpatel
4 Replies
Login or Register to Ask a Question