Change new filename with date ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change new filename with date ??
# 1  
Old 02-08-2006
Change new filename with date ??

Hi all,
I am newbie and hope that you can help me to rename a file
If I have a file name Perform.01222006.12345.Log now I would like to backup another file with another name like perform-20060112.dat

This is a flat file, and I want to collect some field, then put it in a new file from Perform.mmddyyyy.ID.Log to perform-yyyymmdd.dat how do I do that ? can you show me how you write the code.

Thanks,

P/S: I will use ksh shell , and I am sorry if I post in a wrong forum .
# 2  
Old 02-08-2006
It looks like you are asking about five different things.

Sentence #1: Use the mv command to rename files.
Sentence #2: I think you mean copy a file use cp
Code:
cp Perform.01222006.12345.Log perform-20060112.dat

Do you want #1 and #2 as part of the "collect some field". You need to give us exact requirements. Sample of the old file, sample of new file, and file names.
# 3  
Old 02-09-2006
Hi Jim,
Thanks for replying, my case is not sample as mv or cp . I think Smilie .
Okay here is the problem:
In a directory DATA have alot of file name Perform.mmddyyyy.ID.Log where mm is month, dd is day and yyyy is year like Perform.02072006.1234.Log, Perform.02062006.4321.Log, Perform.02082006.4321.Log ...
Each file is a flat file, separate by a pipe like
ID|Location|Date|Hostname|Age|Sex

Then how I write a ksh shell to find all files in DATA directory which are new in the last 2 weeks, and take only 4 fields
ID|Date|Hostname|Age
then write it in a NEWDATA directory, and the file will be named perform-yyyymmdd.dat

Example:
Perform.02062006.4321.Log have
ID|Location|Date|Hostname|Age|Sex
1|SFO|02/06/2006|hawkeye|35|M
2|LAX|02/06/2006|sf49ers|30|M
3|OAK|02/06/2006|goraiders|27|F
4|PIT|02/06/2006|steeler|35|M

and write to a file name perform-20060206.dat
1|02/06/2006|hawkeye|35
2|02/06/2006|sf49ers|30
3|02/06/2006|goraiders|27
4|02/06/2006|steeler|35

How can you do that ? Thanks for sharing your knowlege .
# 4  
Old 02-09-2006
Re:

Hi,

I have solution for this.

Suppose you have a file with "Perform.01222006.12345.Log" and want to rename
as "Perform-01222006.dat".

Solution:
Go to the directory where u have the src-files
under the directory type this command
ls -1 Perform*|awk -F"\." '{print $_ " " $1"-"$2".dat"}'|xargs mv

this will change all the files as per your requirment.

-regards
SubbuMalepati
# 5  
Old 02-09-2006
The OP wants to extract some part of the data and redirect it to another file in another directory. Maybe this is the way to go about it:
Code:
cd /full/path/to/DATA
for file in Perform*Log; do
awk -F\| '{if(NR!=1) print $1"|"$3"|"$4"|"$5}' $file > /full/path/to/NEWDATA/$(echo $i|awk -F. '{print
 tolower($1)"-"$2".dat"}');
done

# 6  
Old 02-09-2006
Thanks for replying and showed me, but
if we use the code
ls -1 Perform*|awk -F"\." '{print $_ " " $1"-"$2".dat"}'|xargs mv

It will give me Perform-mmddyyyy.dat

and if I use:

awk -F\| '{if(NR!=1) print $1"|"$3"|"$4"|"$5}' $file > /full/path/to/NEWDATA/$(echo $i|awk -F. '{print
tolower($1)"-"$2".dat"}');

Then I also get perform-mmddyyyy.dat .

Am I right ?? I need the file name as perform-yyyymmdd.dat
# 7  
Old 02-09-2006
I think the following Script will accomplish the required task. I tried to execute the blowtorch's scripts but it doesn't work so I modified the code to be as follows:
Code:
cd  /full/path/to/DATA
for file in Perform*Log; do
newFile=`ls -1 $file|awk -F. '{ \
	y = substr($2,5,4); \
	m = substr($2,1,2); \
	d = substr($2,3,2); \
	printf "%s-%s%s%s.dat",$1,y,m,d \
	}'`
awk -F\| '{if(NR!=1) print $1"|"$3"|"$4"|"$5}' $file >  /full/path/to/NEWDATA/$newFile
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change existing date to current date in a filename?

Suppose i have a list of files in a directory as mentioned below 1. Shankar_04152019_ny.txt 2. Gopi_shan_03122019_mi.txt 3. Siva_mourya_02242019_nd.txt .. . . . . 1000 . Jiva_surya_02282019_nd.txt query : At one shot i want to modify the above all filenames present in one path with... (4 Replies)
Discussion started by: Shankar455
4 Replies

2. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

3. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
Discussion started by: ust3
7 Replies

4. Shell Programming and Scripting

Change the content of files but not change the date

I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Discussion started by: ust3
0 Replies

5. UNIX for Dummies Questions & Answers

How to change date in a filename?

Hi i want to list files based on date and change the date alone in the files in a directory abc20120101.txt xyzxyxz20120101.txt ccc20120201.txt ddd20120301.txt In the above i want to select only files having date 20120101 and rename the date for those files like below abc20111231.txt... (3 Replies)
Discussion started by: Dewdrop
3 Replies

6. Shell Programming and Scripting

Change the filename variable value

Hi guys, I have a variable where i am storing the filename (with full path). I just need the value before ".txt". But instead of getting the filename i am getting the contents of the filename. FileName=/appl/data/Input/US/Test.txt a=`awk -F"." '{print $1}' ${FileName}` echo $a... (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Shell Programming and Scripting

Get the oldest date based on date in the filename

I am using ksh93 on Solaris. Ok, this may seem like a simple request at first. I have a directory that contains sets of files with a YYYYMMDD component to the name, along with other files of different filespecs. something like this: 20110501_1.dat 20110501_2.dat 20110501_3.dat... (2 Replies)
Discussion started by: gary_w
2 Replies

8. Shell Programming and Scripting

Change the filename

I have 100 files in a directory with a.1 a.2 a.3 a.4 How do i remove a. and i need the file names as 1 2 3 4 please help (2 Replies)
Discussion started by: srichunduru
2 Replies

9. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

10. 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
Login or Register to Ask a Question