用字符串替换文本文件中的第一行 [英] Replace the first line in a text file by a string
本文介绍了用字符串替换文本文件中的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是shell脚本的新手,并对如何使用sed
或任何其他工具用字符串替换文本文件中的第一行感到困惑.这是文本文件的内容:
I am a newbie at shell scripting, and am confused about how to use sed
or any other tools to replace the first line in my text file by a string. Here are the text file contents:
/home/snehil/Desktop/j1/movie.MOV
"spome other text lines'
我只想用movie.MOV
替换第一行(电影文件路径)(可能是shell脚本中的变量)
I want to replace the first line (movie file path) with just movie.MOV
(could be a variable in the shell script)
请指导我如何执行此操作.我在某些帖子中遇到了sed
,我需要在这里使用sed
吗?
Please guide me how to do this. I came across sed
in some posts, do I need to use sed
here?
推荐答案
sed是正确的工具,尝试这样做:
sed is the right tool, try doing :
var="movie.MOV"
sed -i "1s/.*/$var/" file.txt
说明
-
1
表示第一行 - 剩下的就是替换
s///
:我们用$var
变量替换所有(.*)
1
mean first line- the rest is the substitution
s///
: we substitute everything (.*) by the$var
variable
这篇关于用字符串替换文本文件中的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文