如何在 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
热门推荐
4 哈尼族祝福语简短
10 地生中考祝福语简短
11 南宁招生老师祝福语简短
12 滑稽搞笑结婚祝福语简短
13 学生励志祝福语大全简短
14 给侄女的简短祝福语
15 精辟简短的公司祝福语
16 姓陈简短生日祝福语
17 中秋祝福语简短有深度
18 男生的生日祝福语简短