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
  

Wrapping text around a string with sed & functionality

Need to use sed to do a string replacement.

This:


# Comments until end of line


Should be:


<span class=comment>#Comments until end of line</span>


This can be accomplished with sed by using the ‘&’ operator which pastes in the match space.

 

Script started on Mon 01 Aug 2011 04:47:45 PM CDT

deadlycoffee:/tmp #  sed 's/^#.*.$/(&)' test | sed 's/^#$/(&)/' 
(#)
(#)
(#)
this is not a comment
this isn't either
(## This is)
(# So is this.)
deadlycoffee:/tmp #  exit 
exit

Script done on Mon 01 Aug 2011 04:48:52 PM CDT

 

In the above example, I do a much simpler replacement, but it is the same idea.