从文件从另一个多发线替换多行 [英] Replace multiple lines from a file with mutiple lines from another

查看:116
本文介绍了从文件从另一个多发线替换多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在几个命令,搜索和文件替换多个行?

Can I, in few commands, search and replace multiple lines in a file?

我试图以独特的故障切换块从 dhcp_secondary 替换从 dhcp_primary 3故障块,内 dhcp_primary

I'm trying to replace the 3 failover blocks from dhcp_primary by the unique failover block from dhcp_secondary, within dhcp_primary.

我的目标是到dhcpd.conf里从主DHCP这里复制到辅助(信息的 http://www.madboa.com/geek/dhcp-failover/ )。故障转移工作只有配置是相同的,当然,除了故障转移块;正如你所看到的是网站的例子。
所以我想复制这个文件,但是从二级保留故障转移信息。

My goal is to copy the dhcpd.conf from a primary dhcp to the secondary (more information here: http://www.madboa.com/geek/dhcp-failover/). The failover work only if the configuration are identical, except the failover block of course; as you can see is the website's example. So I want to copy this file, but keep the failover information from the secondary.

示例 dhcp_primary

// some lines above
failover peer "A" {
...
}
failover peer "B" {
...
}
failover peer "C" {
...
}
// some lines below

示例 dhcp_secondary

// some different lines above
failover peer "D" {
...
}
// some different lines below

预期的输出必须是:

The expected output have to be:

// some lines above
failover peer "D" {
...
}
// some lines below

我已经可以提取故障切换块:

I already can extract the failover blocks :

awk '/^failover/,/^}$/' dhcp_a

awk '/^failover/,/^}$/' dhcp_b

但我不知道该怎么继续。

But I don't know how to continue.

先谢谢了。

编辑:我的目标更多的细节。

more details for my goal.

推荐答案

您可以试试:

awk -f a.awk dhcp_b dhcp_a

其中, a.awk 是:

/^failover/,/^}$/{
    if (NR==FNR) {
        blk=blk $0 RS
        next
    }
    if (++i==1) {
        printf "%s",blk
    }
    next
}
NR!=FNR{  print }

这篇关于从文件从另一个多发线替换多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆