到目前为止,我已经能够找出如何在文件的开头添加一行,但这并不完全是我想要的。我将用一个例子来说明:
文件内容
some text at the beginning
结果
<added text> some text at the beginning
它很相似,但是我不想用它创建任何新的行…
如果可能的话,我希望用sed来做这件事。
到目前为止,我已经能够找出如何在文件的开头添加一行,但这并不完全是我想要的。我将用一个例子来说明:
文件内容
some text at the beginning
结果
<added text> some text at the beginning
它很相似,但是我不想用它创建任何新的行…
如果可能的话,我希望用sed来做这件事。
当前回答
插入换行符:
sed '1i\\'
其他回答
使用shell:
echo "$(echo -n 'hello'; cat filename)" > filename
不幸的是,命令替换将删除文件末尾的换行符。为了保持它们,人们可以使用:
echo -n "hello" | cat - filename > /tmp/filename.tmp
mv /tmp/filename.tmp filename
既不需要分组,也不需要命令替换。
插入换行符:
sed '1i\\'
有一个非常简单的方法:
echo "your header" > headerFile.txt
cat yourFile >> headerFile.txt
echo -n "text to insert " ;tac filename.txt| tac > newfilename.txt
第一个tac反向传输文件(最后一行先传输),因此“要插入的文本”出现在最后。第2个tac再次将其换行,因此插入的行位于开头,原始文件保持原始顺序。
嗨,用马车回:
sed -i '1s/^/your text\n/' file