mohtashim mohtashim - 1 month ago 15
Bash Question

how to keep file format when using sed editor?

suppose a file that contains:

a=1234
b=5678
c=word


i am writing a bash script that will change a's value. i use following command:

echo `sed "s/\(a=\).*/\1 new/" file` >file


it gives:

a=new b=5678 c=word


i also tried following (using anchors) still getting same format:

echo `sed "s/\(a=\).*[\n]$/\1 new/" file` >file
echo `sed "s/\(a=\).*'\n'$/\1 new/" file` >file


where i want:

a=new
b=5678
c=word


how to do that?

Answer Source

Use the -i argument to edit the file in place:

sed -i 's/\(a=\).*/\1new/' file