侧边栏壁纸
  • 累计撰写 185 篇文章
  • 累计创建 77 个标签
  • 累计收到 17 条评论

目 录CONTENT

文章目录

Linux中通过bash命令如何在文本文件前追加文本内容

码峰
2023-01-17 / 0 评论 / 0 点赞 / 746 阅读 / 150 字 / 正在检测是否收录...
广告 广告

需求描述

在linux写bash脚本时,需要对一个文本文件进行修改,想在文件的前面添加一行,找了一下相关命令,可以使用echo, cat,sed命令实现,记录备忘,供参考。
image-1674030112299

实现方法

echo, cat命令组合

echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt  #方法1 
echo -e "task goes here\n$(cat todo.txt)" > todo.txt    #方法2
echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt  #方法3

sed命令

sed -i '1s/^/task goes here\n/' todo.txt 
0
广告 广告

评论区