Hi,
I would like to replace (part of) a line in a file,
using a shell script
I know how to add a line, simply echo test >> file
But how do I replace a specific line, without touching anything else?
thanks
Solved: Replace a line in a file with shell
Re: Replace a line in a file with shell
You can use sed
It is a very useful program with lots of option (Check the man page > man sed)
this replaces the string 12345678 with the contents of variable $REPLACE in the file $FILE.
the -i option let you edit the file
But if you want to replace a line, and you only know part the line.
Like you want to update a variable
You can use this code
\(variable=\).* searches for a line that starts with variable=
\1 tells to also write the first part (variable= in this case)
and the the new value $NEWVALUE
Hopes this is what you are searching for.
It is a very useful program with lots of option (Check the man page > man sed)
Code: Select all
sed -i "s/12345678/${REPLACE}/g" $FILE
the -i option let you edit the file
But if you want to replace a line, and you only know part the line.
Like you want to update a variable
You can use this code
Code: Select all
sed -i "s/\(variable=\).*/\1${NEWVALUE}/g" $FILE
\1 tells to also write the first part (variable= in this case)
and the the new value $NEWVALUE
Hopes this is what you are searching for.
Re: Solved: Replace a line in a file with shell
Thanks that is exactly what I needed 
