如何在文件开头插入文本? [英] How to insert a text at the beginning of a file?

查看:31
本文介绍了如何在文件开头插入文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经能够找出如何在文件开头添加一行,但这并不是我想要的.我会用一个例子来展示它:

So far I've been able to find out how to add a line at the beginning of a file but that's not exactly what I want. I'll show it with an example:

文件内容

some text at the beginning

结果

<added text> some text at the beginning

它很相似,但我不想用它创建任何新行...

It's similar but I don't want to create any new line with it...

如果可能,我想用 sed 来做到这一点.

I would like to do this with sed if possible.

推荐答案

sed 可以对一个地址进行操作:

sed can operate on an address:

$ sed -i '1s/^/<added text> /' file

您在此处的每个答案中看到的这个神奇的 1s 是什么?线路寻址!.

What is this magical 1s you see on every answer here? Line addressing!.

要在前 10 行添加 吗?

Want to add <added text> on the first 10 lines?

$ sed -i '1,10s/^/<added text> /' file

或者你可以使用命令分组:

Or you can use Command Grouping:

$ { echo -n '<added text> '; cat file; } >file.new
$ mv file{.new,}

这篇关于如何在文件开头插入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆