如何使用sed在文件开头插入一行?

如何使用sed在文件开头插入一行?,sed,Sed,我试图在文件开头插入一行包含以下字符:~o。我正在使用:sed-i''“1s/^/~o\n/”宏,但换行选项根本不起作用。我应该如何改变它?谢谢您可以使用insert命令i: sed '1i\TEXT TO INSERT' file 说明: 1 Addresses the first line i The insert command inserts the following text before(!) line 1 \ Required after i

我试图在文件开头插入一行包含以下字符:
~o
。我正在使用:
sed-i''“1s/^/~o\n/”宏
,但换行选项根本不起作用。我应该如何改变它?谢谢

您可以使用insert命令
i

sed '1i\TEXT TO INSERT' file
说明:

1     Addresses the first line
i     The insert command inserts the following
      text before(!) line 1
\     Required after i (in POSIX compatible versions of sed)
TEXT  The text to insert
例如:

sed '1i\Hello' <<< 'world!'

顺便说一句,
i
甚至可以与换行符一起使用:

sed '1i\Hello\n' <<< 'world'

您可以使用insert命令
i

sed '1i\TEXT TO INSERT' file
说明:

1     Addresses the first line
i     The insert command inserts the following
      text before(!) line 1
\     Required after i (in POSIX compatible versions of sed)
TEXT  The text to insert
例如:

sed '1i\Hello' <<< 'world!'

顺便说一句,
i
甚至可以与换行符一起使用:

sed '1i\Hello\n' <<< 'world'

我回答后测试了你的命令。它对我有用。您遇到了什么问题?当我运行命令时,没有收到任何错误,但字符会附加在文件第一行的开头,而不是单独的一行。所以我想知道为什么\n不起作用?我在给出答案后测试了您的命令。它对我有用。您遇到了什么问题?当我运行命令时,没有收到任何错误,但字符会附加在文件第一行的开头,而不是单独的一行。所以我想知道为什么\n不起作用?抱歉耽搁了,hek2mgl。我尝试了您的建议:sed'1i\~o\n'宏,但我收到了以下错误:sed:1:“1i\~o\n”:在I命令结尾处\之后额外的字符您使用的是哪个版本的sed?哪种操作系统?我不确定这是否是您要找的,但当我编写what/usr/bin/sed时,我收到的是:PROGRAM:sed项目:text_cmds-87抱歉,hek2mgl。我尝试了您的建议:sed'1i\~o\n'宏,但我收到了以下错误:sed:1:“1i\~o\n”:在I命令结尾处\之后额外的字符您使用的是哪个版本的sed?哪个操作系统?我不确定这是否是您要找的,但当我编写what/usr/bin/sed时,我收到的是:PROGRAM:sed项目:text_cmds-87