Help needed with shell script to search and replace a set of strings among the set of files

Hi,

I am looking for a shell script which serves the below purpose.
Please find below the algorithm for the same and any help on this would be highly appreciated.

1)set of strings need to be replaced among set of files(directory may contain different types of files)
2)It should search for the type of files for which replace must be done

Please find below the algorithm for the same and any help on this would be highly appreciated.

######
#below mapped texts must be replaced
######
SEARCH_N_REPLACE={
abc = test1
efg = test2
gfdg = test3
}
######
#directory may contain pdf/txt/jpg files but it should only consider the #mentioned files here {html,htm,css,js}. 
#######

SEARCH_IN_FILES= {html,htm,css,js}


######

sync.log contains file paths as below:

./testing/file.pdf
./sample.jpg
./example.html
./testcondition.css

######

foreach line in sync.log #line means each file path 
foreach ext SEARCH_IN_FILES #should search for extension type of file
if line.ext is ext #if matches the extension
foreach term SEARCH_N_REPLACE
searchterm=parse:SEARCH_N_REPLACE
replaceterm=parse:SEARCH_N_REPLACE
perl -pie 's/searchterm/replaceterm'
perl -pie 's/searchterm/replaceterm' line

Thanks in advance!!!

#!/bin/ksh
cat sync.log | while read line
do
F_EXT=${line##*.}
if [[ ${F_EXT} == "html" || ${F_EXT} == "htm" || ${F_EXT} == "css" || ${F_EXT} == "js" ]]; then
# sed -i 's/abc/test1/g;efg/test2/g;gfdg/test3/g' ${line}
perl -i -pe 's/abc/test1/g;' -pe 's/efg/test2/g;' -pe 's/gfdg/test3/g;' ${line}
fi
done
1 Like

Hi Srini,

Thanks for the script. As the data for the type of files and the replacements will be dynamic, could you please let me know how can i parse the search term and replace among the files as mentioned in the algorithm using for loops.

Thanks a lot for the help .

---------- Post updated at 07:33 AM ---------- Previous update was at 07:05 AM ----------

---------- Post updated at 07:34 AM ---------- Previous update was at 07:33 AM ----------

Im using bash shell. scripting with .sh

---------- Post updated at 07:36 AM ---------- Previous update was at 07:34 AM ----------

Im using bash shell. scripting with .sh

---------- Post updated at 07:50 AM ---------- Previous update was at 07:36 AM ----------

Try :

$ cat log
./testing/file.pdf
./sample.jpg
./example.html
./testcondition.css
$ echo 'gfdg' >example.html
$ cat example.html
gfdg
$ cat test
#!/bin/bash

SEARCH_IN_FILES="html htm css js"
declare -A MYMAP=( [abc]=test1 [efg]=test2 [gfdg]=test3 )

while read line;do

	if grep -w "${line##*.}" <<<$SEARCH_IN_FILES >/dev/null; then
		
		for i in "${!MYMAP[@]}";do
  			 sed -i 's/'"$i"'/'"${MYMAP[$i]}"'/g' $line
		done
	fi

done <"log"
$ bash test
$ cat example.html
test3
1 Like

---------- Post updated 2014-02-27 at 12:49 AM ---------- Previous update was 2014-02-26 at 01:47 PM ----------

$ sh testsync.sh
testsync.sh: line 4: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

not sh its $ bash yourscriptname , which might work with sh also.

getting same error

$ bash testsync
testsync: line 28: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

I tested on this

$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

$ uname -a
Linux xxx.xxx.xxx 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

I am using Cygwin for executing the script. May be the reason for error.

Alternative might work for you

#!/bin/bash

replace(){

# Do entry here abc  = test1
cat <<EOF
abc test1
efg test2
gfdg test3
EOF

}

SEARCH_IN_FILES="html htm css js"


while read line;do

    if grep -w "${line##*.}" <<<$SEARCH_IN_FILES >/dev/null; then
        
        replace | while read i j;do
               		sed -i 's/'"$i"'/'"$j"'/g' $line
        	  done 
    fi

done <"log"
1 Like

---------- Post updated at 01:34 AM ---------- Previous update was at 01:28 AM ----------

Hi Akshay,

Could you please help me with the shell script for deployment if possible?
It is posted as below in the forum:
"Help needed with a shell script for deploying ear file to a weblogic server using WLST "

Thanks in Advance!!