Help with pattern search and return

I would like to write a script which will read a file containing a list of filenames of the format as shown below :

/usr/local/packages/runcmdlinetool
/home/john.doe/sdfsdf/sdfsdfsd/sdfsdf/sdfsdfTemplates.xml
/usr/local/bin/gtar
/home/mark.williams/sdfsdf/sdfsdfsd/sdfsdf/abdccd/sdfsdfTemplates.xml
/home/joe.smith/abcdfcddvdvdvdTemplates.xml
/home/mpt/sdfsdfsdfsdBindings.xml
/home/mark.joe/sdfsd/sdfsdf/sdfsd/sdf/abcddfgdfTemplates.xml
/home/trd/test/bin/sfsdfdsfsdfsdfdsfBindings.xml
/home/test/qa/jdk/bin/packages/jar

There will be some lines in the above file with the file named <randompattern>Templates.xml OR <randompattern>Bindings.xml
The script should search for the word "Templates" or "Bindings" in each line and if present, should return the randompattern existing before these 2 words, which kinda serve as Template or Binding name

Here is my script :

for cfilename in `cat 1.txt | sort -u`
do
filename_w_ext=`basename $cfilename`
export filename_w_ext
filename=`echo $filename_w_ext | sed 's;\(.*\)\..*;\1;'`
export filename
templatename= `echo $filename | awk -FS="Templates" '{print $1}'`
bindingname= `echo $filename | awk -FS="Bindings" '{print $1}'`
echo "filename is : " $filename
echo "templatename is : " $templatename
done

But looks like I am not getting the Tempaltes or Bindings names extracted correctl, some syntax and semantics errors.

As always help is really appreciated.

#!/bin/ksh
typeset -i mCnt
mStr='Templates|Bindings'
while read cfilename
do
  mCnt=`echo ${cfilename} | egrep -c "$mStr"`
  if [ $mCnt -eq 0 ]; then
    continue
  fi
  mFirstPart=`echo ${cfilename} | sed 's;\(.*/\).*;\1;'`
  filename_w_ext=`basename $cfilename`
  filename=`echo $filename_w_ext | sed 's;\(.*\)\..*;\1;'`
  echo "filename = " $filename
  echo "First = "$mFirstPart
done < input_file

My bad, I think I didn't clarify the requirement correctly. Let me illustrate by specifiying an example output :

So the file looks like this

/usr/local/packages/runcmdlinetool
/home/john.doe/sdfsdf/sdfsdfsd/sdfsdf/sdfsdfTemplates.xml
/usr/local/bin/gtar
/home/mark.williams/sdfsdf/sdfsdfsd/sdfsdf/abdccd/sdfsdfTemplates.xml
/home/joe.smith/abcdfcddvdvdvdTemplates.xml
/home/mpt/sdfsdfsdfsdBindings.xml
/home/mark.joe/sdfsd/sdfsdf/sdfsd/sdf/abcddfgdfTemplates.xml
/home/trd/test/bin/sfsdfdsfsdfsdfdsfBindings.xml
/home/test/qa/jdk/bin/packages/jar

The output should ignore all files NOT containing the patterns Templates OR Bindings. But for any line containing either the pattern Templates or Bindings, it should return an output like :

filename is : sdfsdfTemplates
templatename is : sdfsdf

OR if the pattern binding is present then should look like

filename is : sfsdfdsfsdfsdfdsfBindings.xml
bindingname is : sfsdfdsfsdfsdfdsf

I hope that clarifies. Sorry about the verbose post!

I have narrowed down the problem further. I will strip out the absolute directory path for the file and extract a patter like

sfsdfsdfsdTemplates.xml
abcdefgsdfdsBindings.xml

from the list of files.

From this input, how do I produce an output lile

sfsdfsdfsd
abcdefgsdfds

So basically I want to strip out the last part of the filename containing a pattern similar to Tempaltes.xml or Bindings.xml

I prefer awk or sed. Is there substring function in awk or sed which I can use?

thanks,
I

Hope this helps you

sed -e 's/\(.*\)Templates\.xml/\1/g' -e 's/\(.*\)Bindings\.xml/\1/g' filename