Transforming File Names

The following small script simply does a file name replacement. One place where it is useful is when I have a bunch of log files with fully qualified names but then change the server hostname to not be fully qualified and I want all stardard historical information.

#!/bin/ksh

## usage: transtr "Initial String" "Final String" [run]
##
## This command simply transforms a string with the
## default sed substitution.  You must include
## 'run' in the third field for it to execute, otherwise it
## simply returns the two strings.  You could actually
## not specify run and pipe the two fields into another
## command if necessary.
##

if [[ $1 == "" ]]
   then
      grep "^##" $0 | cut -c 3-
      exit
fi

while read STRING
   do
      NEW=`echo $STRING | sed "s/$1/$2/g"`
      echo $STRING $NEW
      if [[ $3 == "run" ]]
	then
       	   mv $STRING $NEW
      fi
   done

Script started on Tue 02 Aug 2011 10:33:28 AM CDT
 #  ls 
20110707_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110708_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110709_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110710_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110711_05_french-roast.deadlycoffee.com_aixbase.sbstats
20110712_05_french-roast.deadlycoffee.com_aixbase.sbstats
 #  ls | transtr ".deadlycoffee.com" "" 
20110707_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110707_05_french-roast_aixbase.sbstats
20110708_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110708_05_french-roast_aixbase.sbstats
20110709_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110709_05_french-roast_aixbase.sbstats
20110710_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110710_05_french-roast_aixbase.sbstats
20110711_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110711_05_french-roast_aixbase.sbstats
20110712_05_french-roast.deadlycoffee.com_aixbase.sbstats 
	 20110712_05_french-roast_aixbase.sbstats
 #  ls | transtr ".deadlycoffee.com" "" run 
20110707_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110707_05_french-roast_aixbase.sbstats
20110708_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110708_05_french-roast_aixbase.sbstats
20110709_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110709_05_french-roast_aixbase.sbstats
20110710_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110710_05_french-roast_aixbase.sbstats
20110711_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110711_05_french-roast_aixbase.sbstats
20110712_05_french-roast.deadlycoffee.com_aixbase.sbstats  
    20110712_05_french-roast_aixbase.sbstats
 #  ls | transtr ".deadlycoffee.com" "" run 

 #  ls 
20110707_05_french-roast_aixbase.sbstats
20110708_05_french-roast_aixbase.sbstats
20110709_05_french-roast_aixbase.sbstats
20110710_05_french-roast_aixbase.sbstats
20110711_05_french-roast_aixbase.sbstats
20110712_05_french-roast_aixbase.sbstats
20110713_05_french-roast_aixbase.sbstats
20110714_05_french-roast_aixbase.sbstats
20110715_05_french-roast_aixbase.sbstats
 #  exit 
exit

Script done on Tue 02 Aug 2011 10:34:13 AM CDT
  

This entry was posted in Uncategorized by admin. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *