Substring/Instring of a string with datestamp

Hi,

I have just started working on unix today. I want to get the instring of a string (filename).

Eg. JAN_BILS_PRINT_01-01-08.txt

Now i want to extract the datestamp from the file and convert the date to the format mm/dd/yyyy.

How do i do this? Please hel pme with this.

Regards,
Saurabh

using bash shell.

bash-3.2$ F="JAN_BILS_PRINT_01-01-08.txt"
bash-3.2$ F=${F:15:8}; F=${F//-//}; F=${F:1:5}20${F:6}; echo $F
01/01/2008
bash-3.2$

Thanks Murphy.

But I am working on ksh shell. The substitution doesnt seem to work on ksh.

(removed)
Just saw I missed the year part ...

It works with ksh93 (a.k.a. /usr/dt/bin/dtksh on Solaris).

A solution with sed:

sed 's!.*_\(..\)-\(..\)-\(..\)..*!\1/\2/\3!'

Regards

An AWK solution

$ echo "JAN_BILS_PRINT_01-01-08.txt" | awk '{ printf("%s/%s/20%s", substr($0,16,2), substr($0,19,2), substr($0,22,2)) }'
01/01/2008
$

echo JAN_BILS_PRINT_01-01-08.txt | cut -b16-23 | sed 's/-/\//g'

Actually my file name is like
JAN_BILS_PRINT_1-01-08.txt

But when i receive the file on later dates (from 10th and later), the file name would be like,
JAN_BILS_PRINT_10-01-08.txt

Therefore I would like to extract the part after the last "_" upto the "." and then change the file format. How can this be done?

Thank you guys....

I followed this approach and it worked :slight_smile:

grep BES_BILLS_RPT_|sed 's/BES\_BILLS\_RPT\_//g'|sed 's/\.csv//g'|sed 's/-/\//g'

Thank you all for ur responses and help.

It works but that's not the most efficient solution...

Regards