shell - to append the file using vi/vim using shell script
问题描述
I'm trying to write a shell which would append a '.txt' file with some data(stored in a variable). This i'm trying to do using 'vi'. I know there are other tools too to append the file...but i need to use vi only
I tried below command, but unfortunately this command is not inserting the data to the end of the file.:
echo $'i{$var}\E:x\n' |vi file.txt
解决方案
使用vi/vim
不允许您就地对文件进行命令行编辑。相反,您可以使用它的命令行等效工具ex
( vi-summary.doc ),它应该在任何符合 POSIX 的 shell 中都可用。
cat file
foo
bar
现在ex
在命令行中使用该实用程序作为
var=dude
printf '%s\n' '$a' "$var" '.' x | ex file
这将就地编辑文件并在文件dude
的最后一行添加文本。
cat file
foo
bar
dude
推荐阅读
- coq - (已解决 - 是一个目标打印错误) Coq - 列表上的简单归纳不接受假设
- python - 从.txt中提取多行到字典
- installation - 如果应用程序已打开并且用户想要卸载它,如何添加警告消息
- firebase - 使用 Firebase 进行 Flutter RealTime Online 和 Offline
- java - 排除来自依赖项的 jar
- router - 路由器的输出缓冲区大小取决于 RTT。如何?
- javascript - touchstart 事件在移动设备上不起作用 - JavaScript
- asp.net-core - OAuth 2.0 与内省 .net core 3.1
- javascript - 使用 Oauth2 登录 Google:错误 400:redirect_uri_mismatch
- javascript - 反应中的异步功能不会对状态变化做出反应