python - 文本处理——两个文本文件 : read block lines from one file and append it after a string in another text file

标签 python bash awk sed

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

我该怎么做?

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
!

等等...直到文件结束。

最佳答案

这是一个包含更多测试样本的 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

关于python - 文本处理——两个文本文件 : read block lines from one file and append it after a string in another text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862058/

相关文章:

bash 在 while 循环中吃字符

command-line - 用于查找顺序无关的唯一对的终端命令

linux - 如何使用 bash 或 awk 在文件中执行关键字段查找?

python - 如何根据图 block 的位置显示图 block ?

python - 将 stats.percentileofscore 按列应用于每一行

git bash 自动完成在 Windows 7 x64 上很慢

regex - 为什么 sed 不像 awk 那样追加换行符?

python - __init__() 得到了一个意外的关键字参数 'text'

python - 检查周围的地雷扫雷pygame?

macos - bash 命令将文件从一台计算机复制到另一台计算机