[英]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. 我想使用sed在文件的最末尾添加一行文本。 In this case I am working on OS X and I want to add a line in the /etc/sudoers files.
在这种情况下,我正在使用OS X,我想在/ etc / sudoers文件中添加一行。 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!). 如果你可以在没有sed的情况下做到这一点就很容易做
echo <line> >> /etc/sudoers
(虽然要注意你有一个有效的行!)。
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. 如果你想用sed做,你可以做
sed -i -e '$a<line>' /etc/sudoers
转到最后一行然后追加你的文字。
Consider using echo
and output redirection: 考虑使用
echo
和输出重定向:
sudo sh -c "echo 'my string' >> /etc/sudoers"
. 。
You can use awk
你可以使用
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
这将增加
text to add
在文件的结尾作为新行file
I have also tried to achieve this using sed, but unfortunately, haven't found any solution yet. 我也尝试使用sed实现这一点,但不幸的是,还没有找到任何解决方案。 But I have found a workaround to achieve this using echo:
但我找到了使用echo实现此目的的解决方法:
$ echo "the line you want to add" >> yourTextFile.txt $ echo“你要添加的行”>> yourTextFile.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.