Sed does not replace lines

I have a file with 1 line of text called output . I have access to the file. I can change it from the editor without any problems.

 $ cat output 1 $ ls -lo* -rw-rw-r-- 1 jbk jbk 2 Jan 27 18:44 output 

What I want to do is replace the first (and only) line in this file with a new value, either 1 or 0. It seems to me that sed should be ideal for this:

 $ sed '1 c\ 0' output 0 $ cat output 1 

But he never changes the file. I tried distributing it in two lines with a backslash and with double quotes, but I can't get it to put 0 (or something else) in the first line.

+4
source share
2 answers

Sed works with threads and outputs its output to standard.

It does not modify the input file.

It is usually used when you want to write its output to a file:

 # # replace every occurrence of foo with bar in input-file # sed 's/foo/bar/g' input-file > output-file 

The above command calls sed on input-file and redirects the output to a new file called output-file .

Depending on your platform, you can use the sed -i option to modify files:

 sed -i.bak 's/foo/bar/g' input-file 

NOTE:

Not all versions of sed -i support.

In addition, different versions of sed implement -i differently.

On some platforms, you MUST specify an extension for backup (on others that you do not need).

+5
source

Since this is an incredibly simple file, sed can really be redundant. It looks like you want the file to have exactly one character: a '0' or '1'.

In this case, it may make sense to simply rewrite the file rather than edit it, for example:

 echo "1" > output 

or

 echo "0" > output 
+2
source

All Articles