linux - How to append string in the contents of files on the basis of search -
so far have tried below unix script append contents of files on basis of search. fine. if run same below script multiple times perl command keep on appending in files.
searcharr=(test1 test2 test3) replacearr=(somestring1 somestring2 somestring3) a=`echo ${#searcharr[@]}` echo $a ((i=0;i<$a;i++)) searchvalue=`echo ${searcharr[i]}` replacevalue=`echo ${replacearr[i]}` file in `find . -name \* -print`; grep "$searchvalue" $file &> /dev/null if [ $? -ne 0 ]; echo "search string not found in $file!" else perl -p -i -e "s/$searchvalue/$replacevalue/g" $file echo $file "replace string success!" fi done done
current output:-
if run more 1 times keep on appending that. example if run 2 times append 2 times
somestring1somestring1test1 somestring2somestring2test2 somestring3somestring3test3
needed output:- no matter how many times runs
somestring1test1 somestring2test2 somestring3test3
add word boundaries sed search pattern.
Comments
Post a Comment