简体   繁体   中英

On Mac OSX how do I add a line to a file using sed when the file is empty?

This works fine

$ sed -i '' '1i\
my text' $HOME/.bashrc

when there is a line one, but has no effect when there is no line one.

One way, will works in both cases :

{ echo my text; cat file; } | tee file

This work for small files, for big files, use a temporary file :

{ echo my text; cat file; } > temp$$ && mv temp$$ file

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