Unable to use SED for quick file editing

Original file

$cat so/app.yaml 
application: SO
...

I run the following command. I get an empty file.

$sed s/SO/so/ so/app.yaml > so/app.yaml 
$cat so/app.yaml 
$

How can you use SED to edit a file without pointing me to an empty file?

+3
source share
4 answers
$ sed -i -e's/SO/so/' so/app.yaml

-i means in place.

+16
source

>used in the pipeline will open the output file when all channels are configured, i.e. before executing the command. Thus, the input file is truncated before execution sed. This is a problem with all shell redirection, not just c sed.

Sheldon Young's answer shows how to use in-place editing.

+5
source

. sed ( s ed), . ed OTOH - , sed, , . ( : ed - , sed - , .)

ed sed ( sed - ), : , . , ed , ed, . , ,, a,b " a b", a 1 ( ), b - $ ( ), " ". s ( ), , sed.

, sed s/SO/so/ ed ,s/SO/so/.

, ed - , - , (w) (q).

:

ed -- so/app.yaml <<-HERE
    ,s/SO/so/
    w
    q
HERE

. .

, , : , . > " , 1 (stdout)". , .. sed, .

sed -i , sed ed, : -, ed, , GNU sed, , GNU. , , GNU, , Solaris, OpenBSD, HP-UX, IBM AIX sed -i.

+5

, , , .

You need to redirect standard output to some temporary file, and when sed will overwrite the original file temporary.

+3
source

Source: https://habr.com/ru/post/1705992/


All Articles