在solaris sed中模式匹配后追加字符串

时间:2022-08-09 00:09:54

I have a file which contains the below

我有一个包含以下内容的文件

hosts:      files
ipnodes:    files
networks:   files
protocols:  files
rpc:        files
ethers:     files
netmasks:   files

I need to append "dns" at the end of only this two lines

我需要在这两行的末尾添加“dns”

hosts:      files dns
ipnodes:    files dns

Solaris servers, having very limited options in sed sed -i is not available looking for a workaround

在sed sed -i中具有非常有限选项的Solaris服务器无法找到解决方法

2 个解决方案

#1


2  

sed without any fancy settings or in place updates would work:

sed没有任何花哨的设置或到位更新将工作:

sed 's/.*/& dns/' file

If you need to make this edit to ONLY lines beginning with lines beginning with hosts and ipnodes, then add a filter to the command:

如果您需要对仅以主机和ipnodes开头的行开头的行进行编辑,则在命令中添加一个过滤器:

sed -e '/^hosts/ s/.*/& dns/' -e  '/^ipnodes/ s/.*/& dns/' file

Note: I am assuming you'll redirect output to a new file, and replace it with the old.

注意:我假设您将输出重定向到新文件,并将其替换为旧文件。

#2


0  

I'm surprised how minimal the Solaris sed really is. I would say use awk in that case but that seems broken as well. My recommendation is to not shell script on Solaris at all. Stop using Solaris and replace it by a reasonable OS if you want to work with shell scripting. Otherwise use a programming language like Python for scripting.

我很惊讶Solaris sed真的很小。我会说在那种情况下使用awk,但这似乎也被打破了。我的建议是不要在Solaris上编写shell脚本。如果要使用shell脚本,请停止使用Solaris并将其替换为合理的操作系统。否则使用像Python这样的编程语言来编写脚本。


Former Answer:

前答案:

The command should look like this:

该命令应如下所示:

sed 's/^\(hosts\|ipnodes\).*/& dns/' file

On Solaris sed the argument to -i is mandatory (unlike GNU sed)

在Solaris sed上,参数为-i是必需的(与GNU sed不同)

sed -i '.backup' 's/^\(hosts\|ipnodes\).*/& dns/' file

That will create a file file.backup. Check man sed!

这将创建一个文件file.backup。检查男人sed!

#1


2  

sed without any fancy settings or in place updates would work:

sed没有任何花哨的设置或到位更新将工作:

sed 's/.*/& dns/' file

If you need to make this edit to ONLY lines beginning with lines beginning with hosts and ipnodes, then add a filter to the command:

如果您需要对仅以主机和ipnodes开头的行开头的行进行编辑,则在命令中添加一个过滤器:

sed -e '/^hosts/ s/.*/& dns/' -e  '/^ipnodes/ s/.*/& dns/' file

Note: I am assuming you'll redirect output to a new file, and replace it with the old.

注意:我假设您将输出重定向到新文件,并将其替换为旧文件。

#2


0  

I'm surprised how minimal the Solaris sed really is. I would say use awk in that case but that seems broken as well. My recommendation is to not shell script on Solaris at all. Stop using Solaris and replace it by a reasonable OS if you want to work with shell scripting. Otherwise use a programming language like Python for scripting.

我很惊讶Solaris sed真的很小。我会说在那种情况下使用awk,但这似乎也被打破了。我的建议是不要在Solaris上编写shell脚本。如果要使用shell脚本,请停止使用Solaris并将其替换为合理的操作系统。否则使用像Python这样的编程语言来编写脚本。


Former Answer:

前答案:

The command should look like this:

该命令应如下所示:

sed 's/^\(hosts\|ipnodes\).*/& dns/' file

On Solaris sed the argument to -i is mandatory (unlike GNU sed)

在Solaris sed上,参数为-i是必需的(与GNU sed不同)

sed -i '.backup' 's/^\(hosts\|ipnodes\).*/& dns/' file

That will create a file file.backup. Check man sed!

这将创建一个文件file.backup。检查男人sed!