Is there a more simple way to do the following below? Maybe a single command that will replace the text if it exists, if not append text to the file. I need to do this for various files and want to find a way to put in in a function. the line formatting will vary slightly between, I put 2 example below.
#If grep exists then sed line, else echo to end of file
grep -q '^sometext' /file1.txt && sed -i "" "s/^sometext.*/sometext $newvar/" /file1.txt || echo "sometext $newvar" >> /file1.txt
grep -q '^someothertext' /file2.txt && sed -i "" "s/^someothertext.*/someothertext=$othernewvar/" /file1.txt || echo "someothertext=$othernewvar" >> /file1.txt