到目前为止,我已经能够找出如何在文件的开头添加一行,但这并不完全是我想要的。我将用一个例子来说明:
文件内容
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来做这件事。
当前回答
别名是另一种解决方案。添加到你的init rc/ env文件:
addtail () { find . -type f ! -path "./.git/*" -exec sh -c "echo $@ >> {}" \; }
addhead () { find . -type f ! -path "./.git/*" -exec sh -c "sed -i '1s/^/$@\n/' {}" \; }
用法:
addtail "string to add at the beginning of file"
addtail "string to add at the end of file"
其他回答
别名是另一种解决方案。添加到你的init rc/ env文件:
addtail () { find . -type f ! -path "./.git/*" -exec sh -c "echo $@ >> {}" \; }
addhead () { find . -type f ! -path "./.git/*" -exec sh -c "sed -i '1s/^/$@\n/' {}" \; }
用法:
addtail "string to add at the beginning of file"
addtail "string to add at the end of file"
插入换行符:
sed '1i\\'
Cat连接多个文件。<()将命令的输出作为文件发送。结合这两者,我们可以在文件的开头和结尾插入行,
cat <(echo "line before the file") file.txt <(echo "line after the file")
注意,在OS X上,sed -i <pattern>文件失败。但是,如果您提供了一个备份扩展名sed -i old <pattern> file,那么file将在file的位置被修改。旧的是创造出来的。然后您可以删除文件。老在你的剧本里。
在文件顶部添加一行:
sed -i '1iText to add\'