简体   繁体   中英

Why sed does not add string in the file line?

I am trying to add a new line with some string ( \\mu ) at the beginning of a file using OSX sed. Now I can do it for common text and numbers. But for symbols it does not work.

sed -i -e '1s/^/\\mu/' file

This adds mu but not \\mu .

I executed sed -i -e '1s/^/\\\\mu/' ./file on OSX 10.13.6 (latest Xtools, I think), Darwin 17.7.0. I did notice that the generation of duplicates files for different variants of the command was anomalous. Let me explain,

sed -i -e 's/^/\\mu/' ./tmp.txt

ie placing "\\mu" on each new line. Output in file "tmp.txt",

    \mulease 192.168.234.5 {
    \mu  starts 3 2018/11/21 18:53:11;
    \mu  ends 3 2018/11/21 18:54:11;
    \mu  tstp 3 2018/11/21 18:54:11;
    \mu  cltt 3 2018/11/21 18:53:11;
etc...

If I execute the command you want,

sed -i -e '1s/^/\\mu/' ./tmp.txt

Output in file "tmp.txt"

    \mulease 192.168.234.5 {
      starts 3 2018/11/21 18:53:11;
      ends 3 2018/11/21 18:54:11;
      tstp 3 2018/11/21 18:54:11;
      cltt 3 2018/11/21 18:53:11;
...etc 

The old file "tmp.txt" is now duplicated and becomes 'tmp 3.txt', whilst the latest version tmp.txt has been correctly edited. Introducing a space into the file name isn't cool ('tmp 3.txt) AND why start at 3? Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM