unix script to ren file use date inside file

December 23, 2010 at 10:29:50
Specs: Windows XP
I have files that contain the following timestamp format inside each file
05-14-2010,06:00:00

Each file contains a different time so

I need to rename the files to passive.YYYYMMDDHHMM

I haven't any idea on how to do this

Any help would be appreciated


See More: unix script to ren file use date inside file

Report •

#1
December 23, 2010 at 10:58:30
I am assuming that myfile.txt has the timestring. From there it's just string parsing and string building:

#!/bin/ksh

orgfile="myfile.txt"
timestamp=$(cat $orgfile)

set - $(IFS=","; echo $timestamp)
yrstr="$1"
timestr="$2"

set - $(IFS="-"; echo $yrstr)
mm="$1" # month
dd="$2" # day
yr="$3" # year

set - $(IFS=":"; echo $timestr)
hh="$1"
min="$2"

# build a new file name
newfile="${yr}${mm}${dd}${hh}${min}"
echo "$newfile"

# change cp to mv when you are sure it works
cp "$orgfile" "$newfile"


Report •

#2
December 23, 2010 at 11:12:29
Thanks for the quick response..

I ran it and got an error on line 5
syntax error at line 5: `timestamp=$' unexpected


Report •

#3
December 23, 2010 at 11:39:52
First, of all do you have ksh actually installed on your linux box? If not, change your shell declaration to #/bin/bash. This script should be portable between ksh and bash.

Second, it looks like the contents of $timestamp are not what you think it is. Remember I said this script only expects the timestamp contents in the file. Blank lines before or after could cause a problem

Execute a:

echo $timestamp

and look at it.


Report •

Related Solutions

#4
December 23, 2010 at 11:47:07
Each file has a timestamp at the begining of each line

Report •

#5
December 23, 2010 at 12:11:18
Concatenating an entire file, with cat, will not work. Since the file has more than one line, choose a method that only grabs the first line. One way is to use sed to print the first line:

timestamp=$(sed -n '1p' "$orgfile")

This script will probably work now, but with out knowing how the time string is situated in the file, I can not be sure.


Report •

#6
December 23, 2010 at 12:24:37
Still getting the same error and I do have ksh installed

Here is the content of a file

05-14-2010,05:15:00,BCP01_0,DBPRFM,4:50-100ms,execute,0,query,0,update,0,batch,0
05-14-2010,05:15:00,BCP01_0,DBPRFM,3:25-50ms,execute,0,query,0,update,0,batch,0
05-14-2010,05:15:00,BCP01_0,DBPRFM,5:100-250ms,execute,0,query,0,update,0,batch,0
05-14-2010,05:15:00,BCP01_0,DBPRFM,0:0-5ms,execute,0,query,29,update,0,batch,0


Report •

#7
December 23, 2010 at 12:48:14
I don't know what to tell you. It works for me using your data. I wrote this on a Solaris box (I don't have ksh for Linux), but this is rather standard ksh/bash scripting. Start trouble shooting by making sure the timestamp variable is the first line of the file:

#!/bin/ksh

orgfile="myfile.txt"
##timestamp=$(cat $orgfile)

timestamp=$(sed -n '1p' "$orgfile")
echo "$timestamp"

set - $(IFS=","; echo $timestamp)
yrstr="$1"
timestr="$2"

set - $(IFS="-"; echo $yrstr)
mm="$1" # month
dd="$2" # day
yr="$3" # year

set - $(IFS=":"; echo $timestr)
hh="$1"
min="$2"

# create a new file name
newfile="${yr}${mm}${dd}${hh}${min}"
echo "$newfile"

# change cp to mv when you are sure it works
cp "$orgfile" "$newfile"

Also, if you have bash installed, try using that.


Report •

#8
December 23, 2010 at 16:13:46
Thanks nails..I have it working..thanks for your time and patience..Great job!!!!

Report •

#9
December 23, 2010 at 16:44:35
One more thing...it's working on a single file...what if I wanted to run it against a number of files all begining with BCP01_0_

Report •

#10
December 23, 2010 at 22:14:24
Thank you for the kind words. First, cd to the directory where your BCP01_)_ files are. Second, place the following for loop around your script, and it should execute for every file found:

#!/bin/ksh

#cd path_to_the_directory_where_you files_are

for orgfile in BCP01_0_*
do
   echo "$orgfile"
   # place your script here
done


Report •

#11
December 24, 2010 at 04:54:21
I hate to bother you again ..this is what I now have and am getting an error on line 5
syntax error at line 5 : `for' unmatched

#!/bin/ksh

#cd path_to_the_directory_where_your_files_are

for orgfile in BCP01_0_*
do
echo "$orgfile"
#script
orgfile="BCP01_0_*"

#timestamp=$(cat $orgfile)

timestamp=$(sed -n '1p' "$orgfile")
echo "$timestamp"

set - $(IFS=","; echo $timestamp)
yrstr="$1"
timestr="$2"

set - $(IFS="-"; echo $yrstr)
mm="$1" # month
dd="$2" # day
yr="$3" # year

set - $(IFS=":"; echo $timestr)
hh="$1"
min="$2"

# create a new file name
newfile="passive.${yr}${mm}${dd}${hh}${min}"
echo "$newfile"

# change cp to mv when you are sure it works
cp "$orgfile" "$newfile"



Report •

#12
December 24, 2010 at 05:40:56
actually after making a few adjustments the script now goes through each file and comes back with the following for each

cp: cannot access BCP01_0_*
BCP01_0_Standard_csv_file20100514@003034416.closed
Can't open BCP01_0_*


Report •

#13
December 24, 2010 at 05:55:57
It's working perfectly now..

I commented out the second

orgfile="BCP01_0_*"

Awesome...thanks for all your help


Report •

Ask Question