15

I know how to add new text to a file, but how can I edit it?

Example: adding hello_world = 1 to test.txt using the following command:

echo "hello_world = 1" >> test.txt

But how can I change 1 to 0 or something else?

Pandya
  • 37,289
Marco98T
  • 367

2 Answers2

35

Using sed:

sed -i 's/1/0/g' test.txt

In general:

sed -i 's/oldstring/newstring/g' filename

See man sed for more info.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
5

Through awk,

awk '{sub(/1/,"0")}1' infile > outfile

Example:

$ echo 'hello_world = 1' | awk '{sub(/1/,"0")}1'
hello_world = 0
Avinash Raj
  • 80,446