Parsing question

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

Thanks but this errors on the first line after 'do', stating bad substitution.

Thanks for this code just prints a "20" and every file name on the same line.

Uh! I wrote "x" variable instead of "xlsFile" :smiley:

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

Also you probably mean "for file in dat*" rather than the silly echo. (^:

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!

Spot on era :smiley:

hi Era,

hope you are doing well, can you answer few threads pls

I am waiting for your kind reply.

Cheers,
Bash