Another awk question

Hello

I'm (another) newbie in bash scripting (my second day).
I'm trying to locate a phrase in a number of files (multiple files in subdirectories) replace this phrase with another one
and produce an output that contains the filename of the search string occurrence, the row in that file and the changed row.

In order to feed with files my search command I use

find "$TopDir" -type f | xargs ......

I have build the following command in order to locate the phrase that I'm looking for and produce FILENAME and line number.

find $SearchDir -type f | xargs awk '/FindMe/ {print FILENAME " " FNR}'> out.txt

I also build the following command in order to replace the FindMe phrase with the WriteMe phrase.

find $SearchDir -type f | while read file
 do
awk '{gsub(/FindMe/,"WriteMe");print}' $file >$file.$$
 mv $file.$$ $file
 done

Trying to add into the out.txt a 3rd column with the changed line is giving a headache.

Any help would greatly appreciated.

Pavlos

Assuming you want to search for the word "FindMe" in the files and replace them with "WriteMe":

#!/bin/ksh

search="FindMe"
replace="WriteMe"
SearchDir="/your/dir"

find $SearchDir -type f | 
while read file
  grep "$search" "$file" > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    awk '$0 ~ s {gsub(s, r)}1' s="$search" r="$replace" "$file" > "${file}".$$
    mv "${file}".$$ "$file"
  fi
done

Thanks for the reply but I'm a little lost.
What the redirection /dev/null 2 is? And a 2nd redirection after that?
Can we direct output in two directions at the some time?

That's my main problem as I cannot figure out how I'm going to produce a report file (filename - file line number - new line content) and on the some time to direct my output to each file that needs to be changed.

> /dev/null
  • direct standard output (which has a file descriptor of 1 (and can also be written as 1> /dev/null)) to the null device.
2>&1
  • copy standard error (which has a file descriptor 2) to standard output (which now points to /dev/null)

(thus sending both standard output and standard error to /dev/null)

You could rewrite that slightly:

if grep -q "$search" "$file"; then
  ...
fi

Finally I have build the following code.

awk produces a file replaced.txt which is a log of the changes that will take place. It logs line, filename and the changed line.

sed makes the changes.

As each file is read twice and I have a few thousands of files I wonder if I can perform the task with one file read.

read SearchDir
read SearchString
read ReplaceString


find "$SearchDir" -type f | 
while read file
do
  awk '$0 ~ /'$SearchString'/ {gsub(s, r); print FNR "\t" FILENAME "\t" $0}' s="$SearchString" r="$ReplaceString" "$file" >> replaced.txt
  sed -i 's/'$SearchString'/'$ReplaceString'/g' "$file"
done