Search and replace in shell scripting

I am trying to write shell script to find and replace using Sed. but i am unable to complete the setting. need help in doing that.

Requirement:

FROM

"${O_INSTANCE}/diag/logs/${C_TYPE}/${C_NAME}/httpd.pid"

TO

"/var/opt/<SID>_<HOSTNAME>/Apache/httpd.pid"

Correction:

search="PidFile ${O_INSTANCE}/diagnostics/logs/${COMPONENT_TYPE}/${COMPONENT_NAME}/httpd.pid"

In order to expand the variables in the search it cannot be surrounded by single quotes.
That's applicable for the sed command as well.
Instead of

grep "$search" $file &> /dev/null
  if [ $? -ne 0 ]; then

You could do

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

Update:

grep "$search" $file > /dev/null

and

sed -i 's?'"$search"'?'"$replace"'?' $file
1 Like

Why not - feasability depending on the probability of files containing "$search" - just

for file in $(find -name 'httpd.conf'); do
   sed -i "s/$search/$replace/" $file
done

EDIT: You may need to replace the slash as a delimiter as suggested by rdrtx1...

Thanks for the help. This worked for me.

---------- Post updated at 08:00 PM ---------- Previous update was at 06:41 PM ----------

I want to replace the text if i find some keyword in the same line. Can someone suggest for that?

sed -i 's?'"${search}"'?'"${replace}"'?' $file

I want to include a keyword and if it finds then only replace should happen.

sed -i '?'"$keyword"'?s?'"${search}"'?'"${replace}"'?' $file
sed -i.bak "\\|${keyword}|s|${search}|${replace}|" file

Your new requirement is not clear. If what Scrutinizer and rdrtx1 suggested don't work for you, we need more information... Do you only want to change a line if that line contains the keyword? Or do you want to change any line in the file if the keyword appears on any line in the file?

Are there any special characters (like forward slashes, question marks, single quotes, double quotes, vertical bars, or parentheses) in the keyword like there are in both the search string and the replacement string?

Both the codes are not working for me.

I need to replace one text in case of multiple occurrences
example :

SSL "none"
none
none is available

So here i want to replace "none" with "nothing" when I find SSL in the same line of none.

For this i need help.

So for this i have used script like this.

search="none"
replace="nothing"
for file in `find -name 'test.txt'`; do
if grep -q "$search" "$file"; then
sed -i  "\\|${SSL}|s|${search}|${replace}|" $file
else
echo "Search string not found in $file!"
fi
done

---------- Post updated at 01:04 PM ---------- Previous update was at 12:57 PM ----------

Hi Don Cragun,

Yes, I want to change a line if that line contains a keyword.

Yes there are special characters in the search and replace.

Here is the original requirement.
Search

SSLMutex "none"

Replace

SSLMutex file:/var/opt/<SID>_<HOSTNAME>/Apache/ssl_mutex

But none keyword is having multiple occurrences. that's the reason i want to use the logic where i can replace a line if that line contains a keyword.

Why not do a search/replace on the entire line?

Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, and adequate (representative) sample input and desired output data and the logics connecting the two, and error messages when availabe, to avoid ambiguities and keep people from guessing.

because you are expandig an uninitialized variable: ${SSL} . Either assign something to the var, or use a string constant:

sed  "\\|SSL|s|${search}|${replace}|" file
SSL "nothing"
none
none is available