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

之前:

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

之后:

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

如你所见,增加了一条新的线路.我的bash脚本如何插入该行?我猜我需要使用sed.

推荐答案

如果要在特定字符串匹配后添加一行:

$ 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

Linux相关问答推荐

X86_64程序集中的分段故障:系统调用问题

如何检测文件系统是否支持权限?

ShellScript,有更好的方法来查找具有特定名称和日期的文件吗?

Linux TTY 操作顺序

使用来自 yocto build 而不是主机系统的 protoc

在不编写任何代码的情况下,是否有一个命令可以检索当前 shell 的亲和力中的可用内核数?

使用 sed 从文本文件中删除特定字符串

从 bash shell 等效项在fish shell 上设置 $ANDROID_SDK_ROOT

ftell 在文件描述符上?

如何使用 sed debug调试正则表达式?

如何更改目录中所有文件中所有出现的单词

如何优雅地退出 X11 事件循环?

是否可以在 Linux x86 GAS 程序集中创建没有系统调用的线程?

yum 可以告诉我哪些存储库提供了特定的包吗?

bash 中的线程?

vscode 总是请求保存权限

限制程序在 Linux 中运行的时间

如何使用终端打开-虚线文件名?

什么是 linux 脚本中的 start-stop-daemon?

Linux 的文本编辑器(除了 Vi)?