Bash:在文件的特定位置插入一行

时间:2022-10-04 01:05:19

I am writing a script that will require me to add lines in a specific part of a config file. For example

我正在编写一个脚本,它要求我在配置文件的特定部分中添加行。例如

Before:

之前:

ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900
ServerActors=UWeb.WebServer

After:

后:

ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.qtracker.com MasterServerPort=27900
ServerActors=UWeb.WebServer

As you can see there is a new line added. How can my bash script insert the line? I'm guessing I will need to use sed.

正如您所看到的,这里添加了一个新行。我的bash脚本如何插入行?我想我需要使用sed。

4 个解决方案

#1


73  

If you want to add a line after a specific string match:

如果您想在特定的字符串匹配之后添加一行:

$ awk '/master.mplayer.com/ { print; print "new line"; next }1' foo.input
ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900
new line
ServerActors=UWeb.WebServer

#2


26  

You can use something like this:

你可以这样使用:

Note that the command must be entered over multiple lines because sed does not allow coding a newline with "\n" or the Ctrl-V/Ctrl-M key combination like some tools. The backslash says "Ignore my hitting the return key, I'm not done with my command yet".

注意,必须通过多行输入命令,因为sed不允许像某些工具一样使用“\n”或Ctrl-V/Ctrl-M组合来编写换行符。反斜杠表示“忽略我的回车键,我还没有完成我的命令”。

sed -i.bak '4i\
This is the new line\
' filename

This should do the trick (It will insert it between line 3 and 4).

这应该可以达到这个目的(它将在第3行和第4行之间插入)。

If you want to put this command itself into a shell script, you have to escape the backslashes so they don't get eaten by bash and fail to get passed to sed. Inside a script, the command becomes:

如果希望将该命令本身放入shell脚本中,则必须避免反斜杠,以免被bash吃掉并无法传递给sed。在脚本中,命令变为:

sed -i.bak '4i\\
This is the new line\\
' filename

#3


11  

awk 'NR==5{print "new line text"}7' file

#4


2  

You can add it with sed in your file filename after a certain string pattern match with:

您可以在与以下字符串模式匹配后,在文件文件名中添加sed:

sed '/pattern/a some text here' filename

in your example

在你的例子

sed '/master.mplayer.com/a \  new line' filename

(The backslash masks the first space in the new line, so you can add more spaces at the start)

(反斜杠覆盖了新行中的第一个空格,因此可以在开头添加更多空格)

source: https://unix.stackexchange.com/a/121166/20661

来源:https://unix.stackexchange.com/a/121166/20661

#1


73  

If you want to add a line after a specific string match:

如果您想在特定的字符串匹配之后添加一行:

$ awk '/master.mplayer.com/ { print; print "new line"; next }1' foo.input
ServerActors=IpServer.UdpServerUplink MasterServerAddress=unreal.epicgames.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master0.gamespy.com MasterServerPort=27900
ServerActors=IpServer.UdpServerUplink MasterServerAddress=master.mplayer.com MasterServerPort=27900
new line
ServerActors=UWeb.WebServer

#2


26  

You can use something like this:

你可以这样使用:

Note that the command must be entered over multiple lines because sed does not allow coding a newline with "\n" or the Ctrl-V/Ctrl-M key combination like some tools. The backslash says "Ignore my hitting the return key, I'm not done with my command yet".

注意,必须通过多行输入命令,因为sed不允许像某些工具一样使用“\n”或Ctrl-V/Ctrl-M组合来编写换行符。反斜杠表示“忽略我的回车键,我还没有完成我的命令”。

sed -i.bak '4i\
This is the new line\
' filename

This should do the trick (It will insert it between line 3 and 4).

这应该可以达到这个目的(它将在第3行和第4行之间插入)。

If you want to put this command itself into a shell script, you have to escape the backslashes so they don't get eaten by bash and fail to get passed to sed. Inside a script, the command becomes:

如果希望将该命令本身放入shell脚本中,则必须避免反斜杠,以免被bash吃掉并无法传递给sed。在脚本中,命令变为:

sed -i.bak '4i\\
This is the new line\\
' filename

#3


11  

awk 'NR==5{print "new line text"}7' file

#4


2  

You can add it with sed in your file filename after a certain string pattern match with:

您可以在与以下字符串模式匹配后,在文件文件名中添加sed:

sed '/pattern/a some text here' filename

in your example

在你的例子

sed '/master.mplayer.com/a \  new line' filename

(The backslash masks the first space in the new line, so you can add more spaces at the start)

(反斜杠覆盖了新行中的第一个空格,因此可以在开头添加更多空格)

source: https://unix.stackexchange.com/a/121166/20661

来源:https://unix.stackexchange.com/a/121166/20661