#!/bin/tcsh

# this script replaces every occurence of REPLACE by BY in the file that is given as first argument
# if no program argument is provided, an error is given and the program stops 
# the powerfull command sed is used

set REPLACE="blo"
set BY="blo"


if ($#argv == 0 ) then
    echo "No arguments supplied...exiting"
    exit 1
else 
    set FILE=$argv[1]

#    if !(-x $FILE) then  # this is to prevent to call sed on executables (files marked with a x)

      
      sed -e "s/$REPLACE/$BY/g w /tmp/sed2.txt" $FILE  > /tmp/sed.txt 
      cp /tmp/sed.txt $FILE
      rm /tmp/sed.txt

      set NBR = `wc -l /tmp/sed2.txt`
      if $NBR[1] > 0 then
        echo "Replaced $NBR[1] lines in file $FILE :"
        more /tmp/sed2.txt
      endif
      rm /tmp/sed2.txt
#   else
#      echo 'sed is not invoked on executables'
#    endif
endif
