search and replace with AWK

Suchen und Ersetzen mit AWK
hello,

i'm not good in scripting and asking for your help.

With this script i change some text parts in diffent datafiles. It works without problems, but i need to get some informations about what changes in wich datafiles happend. This could be in character of a log file oder a simly report after the script has done the changes.

#!/bin/bash
DATMASKE='*.cfg'
read serveralt
read serverneu
echo Bisherige Bezeichnung: $serveralt , neue Bezeichung: $serverneu

for x in $(find /home/test -type f -iname "$DATMASKE"); do
awk ' { gsub("'$serveralt'","'$serverneu'"); print > FILENAME } ' $x

done

I didn't find a solution yet by googling. thanks a lot for your help

ruffi,

I�m not sure if is what you need, but maybe adding at the end of the script a comparison line would help.

Maybe something like

diff $x FILENAME

or

cmp $ FILENAME

This in order to compare original file with the changed one.

Maybe something like this?

#!/bin/bash
DATMASKE='*.cfg'
read serveralt
read serverneu
echo Bisherige Bezeichnung: $serveralt , neue Bezeichung: $serverneu

for x in $(find /home/test -type f -iname "$DATMASKE"); do
  grep -q "$serveralt" "$x"
  if [ $? -eq 0 ]
  then
    awk ' { gsub("'$serveralt'","'$serverneu'"); print} ' "$x" > tempfile
    mv tempfile "$x"
    echo "File $x changed" >> logfile
  fi
done
1 Like

Great, guys ! this is exactly what i needed!:b:
Thanks a lot!