sed not replacing lines

后端 未结 2 1585
余生分开走
余生分开走 2021-01-12 13:16

I have a file with 1 line of text, called output. I have write access to the file. I can change it from an editor with no problems.

$ cat output         


        
相关标签:
2条回答
  • 2021-01-12 14:08

    Since this is an incredibly simple file, sed may actually be overkill. It sounds like you want the file to have exactly one character: a '0' or a '1'.

    It may make better sense in this case to just overwrite the file rather than to edit it, e.g.:

    echo "1" > output 
    

    or

    echo "0" > output
    
    0 讨论(0)
  • 2021-01-12 14:12

    Sed operates on streams and prints its output to standard out.

    It does not modify the input file.

    It's typically used like this when you want to capture its output in a file:

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

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

    Depending on your platform, you might be able to use sed's -i option to modify files in place:

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

    NOTE:

    Not all versions of sed support -i.

    Also, different versions of sed implement -i differently.

    On some platforms you MUST specify a backup extension (on others you don't have to).

    0 讨论(0)
提交回复
热议问题