Issue
hello I am needing help to be able to replace a line of a file with ssh and sed.
The problem is the following I have a string for example:
mystring = 1234
The point is that after the = it is not always 1234, so I am not able to replace that line.
sed -i 's/mystring =/mystring=1234/g' file.ini
when I run it two things happen:
- mystring = 12341234 (add the numbers)
- mystring = 123412345 (don't delete it, leave it there)
and if the variable (sed -i 's/mystring=/mystring=1234/g' file.ini)
is executed more than twice, what it does is add the number at the end.
That is why I need to replace the entire line, something that if it is done more than 1 you see it will always remain the same, and if it is different, change it to the value that is set in the command.
From already thank you very much.
Solution
You can always match and replace the whole line:
sed -i 's/^mystring=.*/mystring=newvalue/' yourfile
.*
in a regular expression matching any number of characters, including none.
You can also c
hange all lines containing a certain pattern:
sed -i '/^mystring=/c\
mystring=newvalue' yourfile
Or with GNU sed:
sed -ie '/^mystring=/c\' -e 'mystring=newvalue' yourfile
Answered By - knittl