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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18272379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:33:56  来源:igfitidea点击:

Bash: Inserting a line in a file at a specific location

linuxbash

提问by dgibbs

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。

采纳答案by Adrian Frühwirth

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

回答by MPrazz

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

回答by Kent

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

回答by rubo77

You can add it with sedin your file filenameafter a certain string patternmatch with:

您可以sedfilename某个字符串pattern匹配后将其添加到您的文件中:

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