如何在 Linux 中的文件开头插入文本?
为了在文本文件的开头插入文本,我们必须熟悉sed命令,因为sed命令可以用来解决上述问题。
让我们首先探索sed命令,它是流编辑器的缩写。此命令用于对特定文件执行不同的功能,如查找、替换、插入等。
考虑我们有一个目录d1,其中存在两个.txt文件,该目录看起来像这样-
immukul@192 d1 % ls -ltr total 8280 -rwxrwxrwx 1 immukul staff 4234901 Jul 7 17:41 file.txt -rw-r--r-- 1 immukul staff 105 Jul 16 09:30 somefile.txt
现在我们要更改名为somefile.txt的文件的内容,并在.txt文件的第一行插入一些我们选择的文本。
somefile.txt在文件中插入任何内容之前的内容看起来像这样
immukul@192 d1 % cat somefile.txt this file contains a new text stream and i am going to edit that stream yes i can do that ask jeffrey
现在为了向第一行添加一些文本,我们将使用如下所示的命令
命令
对于Mac操作系统
sed -i "" '1s/^/Is this text added\n/' somefile.txt
对于Ubuntu/Fedora
sed -i '1s/^/Is this text added\n/' somefile.txt输出结果
immukul@192 d1 % cat somefile.txt Is this text added this file contains a new text stream and i am going to edit that stream yes i can do that ask jeffrey
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下