0

using Centos7

e.g

vi test.txt 

test.txt contains the following information

x=100
y=200
z=300

I want to put a command into the CLI such as echo x=250 >> test.txt but instead of x=250 being added to the bottom of the file, I want it to replace x=100 with x=250

any help on hwo to tackle this is much appreciated!

Thanks.

1 Answers1

0

You can use sed's inline replacement feature for this:

Like:

sed -i 's/x=.*$/x=250/g' test.txt

For example:

mtak@rubiks:~$ more test.txt
x=100
y=200
z=300
mtak@rubiks:~$ sed -i 's/x=.*$/x=250/g' test.txt
mtak@rubiks:~$ more test.txt
x=250
y=200
z=300
mtak
  • 17,262