Hi Guys,
I was wondering if you could help me out -
I have a directory /home/users/datafiles/ which contain files "dat dd-mm-yy.xls"
I am trying to write a script which does the following -
(1) loops through all the files
(2) retrieves the dd-mm-yy string and converts it into a yyyymmdd to be stored as a variable
for xlsFile in dat*.xls
do
##scripting
echo $xlsDate
echo $formattedDate
echo $xlsFile
done
E.g. If I had file "dat 10-01-08.xls", these 3 would be the result -
10-01-08
20080110
dat 10-01-08.xls
Thanks in advance,
Just re-bouncing this post. I would appreciate any help.
Please, don't rebounce your threads.
For your question, try something like this:
for xlsFile in dat*.xls
do
d=${x:4:2}
m=${x:7:2}
y=${x:10:2}
xlsDate="$d-$m-$y"
formattedDate="20$y$m$d"
echo $xlsDate
echo $formattedDate
echo $xlsFile
done
Obviously if $y is greater than a certain (imposed) value you need to add an if statement and use "19" instead of "20" in the formattedDate expression.
for file in "$(echo dat*)"
do
xlsdate=$(echo $i | awk '{split($2,a,"\.xls");print a[1]}')
fmtdate=$(echo $xlsdate | awk -F"-" '{for(i=NF;i>=1;i--) s=s""$i;print "20"s}')
echo $xlsdate
echo $fmtdate
echo $file
done
robotronic:
Please, don't rebounce your threads.
For your question, try something like this:
for xlsFile in dat*.xls
do
d=${x:4:2}
m=${x:7:2}
y=${x:10:2}
xlsDate="$d-$m-$y"
formattedDate="20$y$m$d"
echo $xlsDate
echo $formattedDate
echo $xlsFile
done
Obviously if $y is greater than a certain (imposed) value you need to add an if statement and use "19" instead of "20" in the formattedDate expression.
Thanks but this errors on the first line after 'do', stating bad substitution.
shamrock:
for file in "$(echo dat*)"
do
xlsdate=$(echo $i | awk '{split($2,a,"\.xls");print a[1]}')
fmtdate=$(echo $xlsdate | awk -F"-" '{for(i=NF;i>=1;i--) s=s""$i;print "20"s}')
echo $xlsdate
echo $fmtdate
echo $file
done
Thanks for this code just prints a "20" and every file name on the same line.
Uh! I wrote "x" variable instead of "xlsFile"
for xlsFile in dat*.xls
do
d=${xlsFile:4:2}
m=${xlsFile:7:2}
y=${xlsFile:10:2}
xlsDate="$d-$m-$y"
formattedDate="20$y$m$d"
echo $xlsDate
echo $formattedDate
echo $xlsFile
done
Also, this works only with bash. You didn't specified which shell are you using. If not using bash, you may extract the same values through "cut" commands or something similar.
There was a typo...xlsdate changed from "echo $i" to "echo $file"
for file in "$(echo dat*)"
do
xlsdate=$(echo $file | awk '{split($2,a,"\.xls");print a[1]}')
fmtdate=$(echo $xlsdate | awk -F"-" '{for(i=NF;i>=1;i--) s=s""$i;print "20"s}')
echo $xlsdate
echo $fmtdate
echo $file
done
era
May 13, 2008, 2:34pm
9
Also you probably mean "for file in dat*" rather than the silly echo. (^:
robotronic:
Uh! I wrote "x" variable instead of "xlsFile"
for xlsFile in dat*.xls
do
d=${xlsFile:4:2}
m=${xlsFile:7:2}
y=${xlsFile:10:2}
xlsDate="$d-$m-$y"
formattedDate="20$y$m$d"
echo $xlsDate
echo $formattedDate
echo $xlsFile
done
Also, this works only with bash. You didn't specified which shell are you using. If not using bash, you may extract the same values through "cut" commands or something similar.
I used -
d=$(echo $xlsFile | cut -c4-5)
m=$(echo $xlsFile | cut -c7-8)
y=$(echo $xlsFile | cut -c10-11)
and it worked okay. Thanks!
This didn't work for me so I gave up trying to fix it.
Also, I was using Korn shell.
Thanks!
hi Era,
hope you are doing well, can you answer few threads pls
Hi, i have a small query about sendmail, my all email are store in /var/spool/mail/test1 while test1 is the name of user, he is using that account from one year and all his emails are copied in server | The UNIX and Linux Forums
I am waiting for your kind reply.
Cheers,
Bash