parse_filename (version 1.1)

You can also look at:
#! /usr/bin/ksh

############################################################
# parse a filename to get a certain value
#
# SYNTAX parse_filename value filename
#
# VERSION: 1.1
#
# HISTORY: 0.0 -> 1.0 12/20/96
# HISTORY: added mathod for parsing source number
# HISTORY: 
# HISTORY: 1.0 -> 1.1 2/7/97
# HISTORY: now able to handle three character instrument names
# HISTORY: (e.g. sis, gis ) 
#
############################################################

value=$1
file=$2

case "$file" in

ad????????[sg]i*)
     ##################################
     # three character instrument name
     ##################################
     afterinst=${file#ad???????????}
     ;;

*)
     ################################
     # two character instrument name
     ###############################
     afterinst=${file#ad??????????}
     ;;
esac


######################
# parse name
######################

case "$value" in

inst)
     #########################
     # instrument name
     ########################
     dum=${file#ad????????}
     echo ${dum%${afterinst}}
     ;;

index)
     #########################
     # file index
     ########################
     tail=${afterinst#???}
     echo ${afterinst%${tail}}
     ;;

mode)
     #########################
     # observing mode
     ########################
     dum=${afterinst#???}
     tail=${dum#??}
     echo ${dum%${tail}}
     ;;

bitrate)
     #########################
     # instrument name
     ########################
     dum=${afterinst#?????}
     tail=${dum#?}
     echo ${dum%$tail}
     ;;

source)
     #######################
     # source number
     #######################
     dum=${file%.*}
     dum=${dum##*_}
     echo $dum
     ;;

*)
     #######################
     # unknown value type
     #######################
     exception 1 "can't extract $value from filename $2";;

esac