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 .
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
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.
Hi Jim,
Thanks for replying, my case is not sample as mv or cp . I think .
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 .
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
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:
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
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
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:
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
Thank you very much , I have learned it from you
Here is the code if someone want to learn
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",tolower($1),y,m,d \
}'`
awk -F\| '{if(NR!=1) print $1"|"$3"|"$4"|"$5}' $file > /full/path/to/NEWDATA/$newFile
done
Thanks again
Just wonder if we look at the flie
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
The field $3 is date 02/06/2006 and we write the output is file name perform-20060206.dat, its contain
1|02/06/2006|hawkeye|35
2|02/06/2006|sf49ers|30
3|02/06/2006|goraiders|27
4|02/06/2006|steeler|35
Any idea ? I just want to learn with diffrerent way .
Thanks,
echo '02/06/2006' | nawk '{n=split($1, da, "/"); print da[3] da[1] da[2]}'