I know sed can replace a piece of string within a text file like this :
sed -i 's/oldstring/newstring/g' test.txt
But, how do I replace the line if it exists and automatically add it on a new line if it doesn't exist?
Edit : Thank you for your responses. As per the requests on your comments and answer, here's more details :
- I want to check the existence of the old string and replace it with the new string if it exists.
- I want to add the new string in a new line at the end of the txt file if the old string does not exist
- If there are multiple occurances of the same string, it would be an error since its a config file.The other occurances can be removed after replacing the first occurance.