简体   繁体   中英

Sed command to add line of text to the very end of a file

I want to use sed to add a line of text to the very end of a file. In this case I am working on OS X and I want to add a line in the /etc/sudoers files. I want my string to be the last string in the file on its own line. Any ideas?

Example:

text 
text 
text
<my string>

Thanks.

If you can do it without sed it would be easy enough to do echo <line> >> /etc/sudoers (though be careful that you have a valid line!).

If you want do it with sed you could do sed -i -e '$a<line>' /etc/sudoers to go to the last line then append your text.

Consider using echo and output redirection:

sudo sh -c "echo 'my string' >> /etc/sudoers"

.

You can use awk

awk '1; END {print "text to add"}' file > tmp && mv tmp file

This will add text to add as a new line at the end of file file

I have also tried to achieve this using sed, but unfortunately, haven't found any solution yet. But I have found a workaround to achieve this using echo:

$ echo "the line you want to add" >> yourTextFile.txt

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