Replace file name on certain "changeable string"

Dear all,

I can use the following script to remove certain strings of my filename.

for filename in `find . -name *blah*`;
do
  new_name=`echo $filename | sed -e "s/blah//g" $filename`
  mv $filename $new_name
done

What if i had file names below and i wish to sed only certain part of the filename?

oringinal filename:

20111111_DATA_ABC_ABC_DEF_END1.bin
20111111_DATA_BCD_EDF_AAS_END2.bin

final filename:

20111111_DATA__END1.bin
20111111_DATA__END2.bin

Thanks and looking forward to hear from you.

This is the way that I'd do it:

  find . -name "*stuff* | awk '
    {
        n = split( $1, a, "_" );
        printf( "mv %s %s_%s__%s\n", $1, a[1], a[2], a[n] );
    }
  ' # ksh    ### remove the comment before ksh after you verify that the commands generated are what you want. 

This will generate the move commands. After you verify that they are correct, you can pipe the output directly to ksh or bash

echo 20111111_DATA_ABC_ABC_DEF_END1.bin |awk '{print $1,$2,$NF}' FS=_ OFS=_

20111111_DATA_END1.bin

Hi rdcwayx,

Thanks alot. What is the meaning of $NF FS=_ and OFS_ ?

I am new to shell.

Field Splitting Summary - The GNU Awk User's Guide
Fields - The GNU Awk User's Guide

Thanks..I found it already. Its awk program , where FS =field seperator and OFS is output field seperator. I learned something new today!! Thanks alot guys~~~