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?
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.
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