我需要合并两个有固定行块的文本文件为一个。
我该怎么做呢?
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
!etc...until文件的结尾。
发布于 2019-07-03 11:59:56
下面是一个具有更广泛测试样本的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.1input.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 endscript.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发布于 2019-07-03 09:26:43
这可能对您有用(GNU sed):
sed -e '12~12{:a;R file2' -e 'x;s/^/x/;/x\{6\}/!{x;ba};z;x}' file1这将按顺序从file2读取file1中的每12行6行。它在保持空间中使用一个计数器,该计数器在第六行后重置。
发布于 2019-07-03 10:55:41
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。
https://stackoverflow.com/questions/56862058
复制相似问题