【问题标题】:Text Processing - Two text files: read block lines from one file and append it after a string in another text file文本处理 - 两个文本文件:从一个文件中读取块行并将其附加到另一个文本文件中的字符串之后
【发布时间】:2019-11-13 16:01:44
【问题描述】:

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

我该怎么做?

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
!

所以基本上我需要从 text2.txt 中每 6 行读取一次,然后每 12 行将其附加到 text1.txt 中

期望的输出:

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
!

等...直到文件结束。

【问题讨论】:

    标签: python bash awk sed


    【解决方案1】:

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

    sed -e '12~12{:a;R file2' -e 'x;s/^/x/;/x\{6\}/!{x;ba};z;x}' file1
    

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

    【讨论】:

      【解决方案2】:
      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 的问题):

      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。

      【讨论】:

        【解决方案3】:

        这是一个awk 脚本,其中包含更广泛的测试样本。

        input.1.txt

        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

        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

        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
        }
        

        运行:

        awk -f script.awk input.2.txt input.1.txt
        

        输出:

        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
        

        【讨论】:

        • 这在inpSectn = inpSectn $0; 中缺少 ORS,如果 /when input.2.txt 中的块数小于 input.1.txt 中的数,则将打印一个空行。见stackoverflow.com/a/56868537/1745001
        【解决方案4】:

        谢谢大家!正如我所见,有很多方法可以做到这一点。我使用了 awk 脚本,但 Ed Morton 和 potong 方法也可以! 谢谢。

        【讨论】:

        • 因为您的声誉得分为 15,所以您可以为贡献者投票。
        猜你喜欢
        • 2013-05-02
        • 2016-04-29
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 1970-01-01
        相关资源
        最近更新 更多