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.

  • I don't understand why you ask for "how to echo" when you actually want to replace texts. There are already so many duplicates which can easily be find within the first google page [Find and replace text within a file using commands](https://askubuntu.com/q/20414/253474), [Find and replace text strings](https://superuser.com/q/548995/241386), [How can I replace a string in a file(s)?(https://unix.stackexchange.com/q/112023/44425), [Replacing string in a script using sed](https://superuser.com/q/778344/241386)... – phuclv Jan 16 '18 at 15:53
  • echo then amend was my train of thought, I did search on google but was obviously using the wrong language. thanks for your reply. – davetherave Jan 16 '18 at 16:48

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
  • 16,513
  • 2
  • 52
  • 64
  • @davetherave I'm glad it does, please mark it as an answer by clicking the green check mark on the left, so it's easier for posterity to find (and give me a few rep in the process). See https://superuser.com/help/someone-answers – mtak Jan 17 '18 at 08:02