雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

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

2024-07-06 52

需求描述

在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 
更新于:2个月前
赞一波!

文章评论

全部评论