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.
Example
/s1/arch/storage/pippo_pluto_20101212.txt

or

/s1/arch/storage/history/pippo_pluto_20101212.txt

Any idea?
Thank you to evryone


See More: extract date from a file name in ksh

Report •

#1
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:

#!/bin/ksh

# 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 '.'
   v=${v%.$x}
echo $v
}

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

var="/s1/arch/storage/history/pippo_pluto_20101212.txt"
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 •

#2
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