User_890
User_890

Reputation: 189

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?

Upvotes: 1

Views: 479

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133528

You could a following sed too once.

sed  -i '/^a=/s/=.*/=new/g'   Input_file

Upvotes: 0

arco444
arco444

Reputation: 22831

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

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

Upvotes: 2

Related Questions