parse_filename (version 2.0)

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

##############################################################################
#
# SYNTAX: parse_filename value filename
#
# DESCRIPTION: This utility extracts information from a file name.
#
# VERSION: 2.0
#
# HISTORY: 0.0 -> 1.0 12/20/96
# HISTORY: Added method 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).
# HISTORY: 
# HISTORY: 1.1 -> 1.2 1998-08-25
# HISTORY: Supplied missing "$UTIL" to exception call
# HISTORY: 
# HISTORY: 1.2 -> 2.0 1999-05-07
# HISTORY: Converted from ksh to perl
#
# CALLS: $UTIL/exception
#
##############################################################################

($value, $file) = @ARGV;

$UTIL = $ENV{"UTIL"};

if ( $file =~ /ad........[sg]i.*/ ) {
    ##################################
    # Three character instrument name
    ##################################
    ($afterinst = $file) =~ s/^ad...........//;
}
else {
    ################################
    # Two character instrument name
    ################################
    ($afterinst = $file) =~ s/^ad..........//;
}


#############
# Parse name
#############
if( $value eq "inst" ) { ($name = $file) =~ s/^ad........(.*)$afterinst/$1/; }
elsif( $value eq "index"   ) { $name=substr($afterinst, 0, 3); }
elsif( $value eq "mode"    ) { $name=substr($afterinst, 3, 2); }
elsif( $value eq "bitrate" ) { $name=substr($afterinst, 5, 1); }
elsif( $value eq "source"  ) { ($name = $file) =~ s/^.*_(.*)\..*$/$1/; }
else {
    #####################
    # Unknown value type
    #####################
    `$UTIL/exception $0 1 "Cannot extract $value from filename $file"`;
}
    
if($name ne "" ) { print "$name\n"; }

exit 0;