extract date from a file name in ksh

December 23, 2010 at 02:23:10
Specs: Windows XP
Hi to everyone,

i'm trying in Linux ksh to catch the date from the name of a file, the problem is that can change the directory where the file is sotred so also the length of the name of input.



Any idea?
Thank you to evryone

See More: extract date from a file name in ksh

Report •

December 23, 2010 at 09:18:40
The Linux and Unix dirname and basename commands are the basis for this. Let me know if you have any questions:


# Using ksh pattern-matching operators, this function# replaces the unix basename command.
# exists, strip off the extension.
function my_basename
{   typeset v x   v=${1##*/}   x=${2#.}
   # get rid of the '.'
echo $v

# Using ksh pattern-matching operators, this function
# replaces the unix dirname command.
function my_dirname {
   typeset v="$@"
   echo ${v%/*}

basefile=$(my_basename $var "txt")

echo "basefile is: $basefile"

# parse the file string:
set - $(IFS="_"; echo $basefile)

# last arg - which is 3rd arg in the example - is the date string:
eval fdate=\$$#
echo $fdate

Report •

December 24, 2010 at 00:05:18
Oh thank you very much nails, it's all clear.

It works good :).

Thank you again.

Report •
Related Solutions

Ask Question