Is it possible to either find and replace a line in a file OR append a string to the end if it is not there?
I know I can use this to find and replace:
sed -i -e "s/^SEARCH/LINE 1\nLINE 2/" file
I know I can append to the file like this:
cat << EOF | tee -i file1 file2
LINE 1
LINE 2
EOF
Is it possible to somehow to combine this. So if /^SEARCH.*$/
matches then replace it, if it doesn't, then append the replacement to the end of the file.
Update with a better input/output example:
For example, if I had this input file testfile
:
Alpha
Bravo
Charlie
Let's say I wanted to find and replace Bravo
with Bravo=bingo
, OR add Bravo=bingo
if Bravo
is not there, the expected output is:
Alpha
Bravo=bingo
Charlie
This is because Bravo
exists in the file, so it is replaced.
Let's say I wanted to find and replace Delta
with Delta=bingo
, OR add Delta=bingo
if Delta
is not there, the expected output is:
Alpha
Bravo
Charlie
Delta=bingo
This is because Delta
is not in the file so it is appended.