首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本处理-两个文本文件:从一个文件中读取块行,并在另一个文本文件中的字符串后追加它。

文本处理-两个文本文件:从一个文件中读取块行,并在另一个文本文件中的字符串后追加它。
EN

Stack Overflow用户
提问于 2019-07-03 02:17:56
回答 4查看 81关注 0票数 1

我需要合并两个有固定行块的文本文件为一个。

我该怎么做呢?

代码语言:javascript
复制
bridge-domain AAAA
mac
aging
time 3
!
limit
maximum 12
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop  <--- 

在这行之后,我需要从另一个文本文件中追加一个代码块,如下所示:

代码语言:javascript
复制
interface Bundle-Ether AAAAA
igmp snooping profile igmp-snoop
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
!

因此,基本上,我需要从text2.txt中每读6行,并将其每12行附加到text1.txt中。

减少的产出:

代码语言:javascript
复制
bridge-domain AAAA
mac
aging
time 3
!
limit
maximum 12
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop
interface Bundle-Ether AAAAA
igmp snooping profile igmp-snoop
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
!
bridge-domain BBBB
mac
aging
time 3
!
limit
maximum 12
notification both
!
port-down flush disable
!
igmp snooping profile igmp-snoop
interface Bundle-Ether BBBB
igmp snooping profile igmp-snoop
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
!

etc...until文件的结尾。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-07-03 11:59:56

下面是一个具有更广泛测试样本的awk脚本。

input.1.txt

代码语言:javascript
复制
bridge-domain AAAA
mac
aging
time 3
!
limit
maximum 12
notification both
!
port-down flush disable
! -1
igmp snooping profile igmp-snoop  1
! 1.1
bridge-domain BBBB
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -2
igmp snooping profile igmp-snoop 2
! 2.1
bridge-domain CCC
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -3
igmp snooping profile igmp-snoop 3
! 3.1

input.2.txt

代码语言:javascript
复制
interface Bundle-Ether AAAAA
igmp snooping profile igmp-snoop
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! AAAA section end
igmp snooping profile igmp-snoop
interface Bundle-Ether BBBB
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! BBBB section end
igmp snooping profile igmp-snoop
interface Bundle-Ether CCCC
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! CCCC section end

script.awk

代码语言:javascript
复制
FNR == NR {    # read insertion paragraph from file 1
    inpSectn = inpSectn $0; # accumlate input lines in inpSectn
    if (NR % 6 == 0) {  # if 6th line add section to array
        sectnArr[++arrCount] = inpSectn; # add inpSectn to ordered array
        inpSectn = "";  # reset inpSectn
    }
    next;      # skip further processing till all file 1 is consumed.
}
1              # output current input line.
FNR % 12 == 0 {   # every 12th line in file 2
    print sectnArr[++arrIdx]; # output section
}

跑步:

代码语言:javascript
复制
awk -f script.awk input.2.txt input.1.txt

产出:

代码语言:javascript
复制
bridge-domain AAAA
mac
aging
time 3
!
limit
maximum 12
notification both
!
port-down flush disable
! -1
igmp snooping profile igmp-snoop  1
interface Bundle-Ether AAAAA
igmp snooping profile igmp-snoop
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! AAAA section end
! 1.1
bridge-domain BBBB
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -2
igmp snooping profile igmp-snoop 2
igmp snooping profile igmp-snoop
interface Bundle-Ether BBBB
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! BBBB section end
! 2.1
bridge-domain CCC
mac
aging
time 3
!
limit
maximum 12
notification both
port-down flush disable
! -3
igmp snooping profile igmp-snoop 3
igmp snooping profile igmp-snoop
interface Bundle-Ether CCCC
dhcp ipv4 snoop profile
static-mac-address 0001
static-mac-address 0002
! CCCC section end
! 3.1
票数 1
EN

Stack Overflow用户

发布于 2019-07-03 09:26:43

这可能对您有用(GNU sed):

代码语言:javascript
复制
sed -e '12~12{:a;R file2' -e 'x;s/^/x/;/x\{6\}/!{x;ba};z;x}' file1

这将按顺序从file2读取file1中的每12行6行。它在保持空间中使用一个计数器,该计数器在第六行后重置。

票数 1
EN

Stack Overflow用户

发布于 2019-07-03 10:55:41

代码语言:javascript
复制
awk '
    NR==FNR {
        rec = rec $0 ORS
        if ( (FNR % 6) == 0 ) {
            recs[FNR/6] = rec
            rec = ""
        }
        next
    }
    { print }
    (FNR % 12) == 0 ) { printf "%s", recs[FNR/12] }
' file2 file1

或者,如果您喜欢简短而神秘的(因为您要求一个sed解决方案,而不是简单的s/old/new):

代码语言:javascript
复制
awk 'NR==FNR{r=r$0"\n";if(!FNR%6){s[++x]=r;r="";next}!(FNR%12){$0=$0 s[++y]}1' file2 file1

上面的内容将在每个UNIX机器上使用任何shell中的任何awk。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56862058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档