如何将第一行与接下来的几行组合起来?

如何将第一行与接下来的几行组合起来?

我已经过滤了数据并制作了一个 CSV 文件,如下所示:

%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="20.20.20.20", MAXHOP=10;%%
1,3,1,1,1,10.10.10.10
2,3,1,1,1,10.37.112.5
3,0,NULL,NULL,NULL,Timeout
4,0,NULL,NULL,NULL,Timeout
5,0,NULL,NULL,NULL,Timeout
6,3,1,1,1,20.20.20.20
%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
1,3,1,1,1,30.30.30.30
2,3,1,1,1,30.30.30.30
3,3,1,1,1,30.30.30.30
4,0,NULL,NULL,NULL,Timeout
5,3,1,1,1,40.40.40.40
6,3,1,1,1,40.40.40.40
7,3,1,1,1,40.40.40.40
%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
1,3,1,10,1,50.50.50.50
2,3,1,1,1,50.50.50.50
3,0,NULL,NULL,NULL,Timeout
4,3,1,1,1,50.50.50.50
5,3,1,1,1,50.50.50.50
6,3,1,1,1,60.60.60.60
7,3,1,1,1,60.60.60.60
8,3,1,1,1,60.60.60.60

我希望每个 %%TRACERT 行都填充这样的跃点:

1,3,1,1,1,10.10.10.10,%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="20.20.20.20", MAXHOP=10;%%
2,3,1,1,1,10.37.112.5,%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="20.20.20.20", MAXHOP=10;%%
3,0,NULL,NULL,NULL,Timeout,%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="20.20.20.20", MAXHOP=10;%%
4,0,NULL,NULL,NULL,Timeout,%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="20.20.20.20", MAXHOP=10;%%
5,0,NULL,NULL,NULL,Timeout,%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="20.20.20.20", MAXHOP=10;%%
6,3,1,1,1,20.20.20.20,%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="20.20.20.20", MAXHOP=10;%%
1,3,1,1,1,30.30.30.30,%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
2,3,1,1,1,30.30.30.30,%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
3,3,1,1,1,30.30.30.30,%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
4,0,NULL,NULL,NULL,Timeout,%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
5,3,1,1,1,40.40.40.40,%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
6,3,1,1,1,40.40.40.40,%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
7,3,1,1,1,40.40.40.40,%%TRACERT: IPTYPE=IPv4, LOCALIP4="30.30.30.30", PEERIP4="40.40.40.40", MAXHOP=10;%%
1,3,1,10,1,50.50.50.50,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
2,3,1,1,1,50.50.50.50,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
3,0,NULL,NULL,NULL,Timeout,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
4,3,1,1,1,50.50.50.50,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
5,3,1,1,1,50.50.50.50,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
6,3,1,1,1,60.60.60.60,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
7,3,1,1,1,60.60.60.60,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%
8,3,1,1,1,60.60.60.60,%%TRACERT: IPTYPE=IPv4, LOCALIP4="50.50.50.50", PEERIP4="60.60.60.60", MAXHOP=10;%%

答案1

因此,请保存以以下开头的行%%TRACRT:,然后将它们附加到以下留置权:

awk '/^%%TRACERT:/ {sfx = $0; next} {print $0 "," sfx}' data

答案2

使用 any sed,您可以将%%TRACERT行存储在h旧空间中并d删除它们,而其他行则获得附加的保留空间G,并用逗号替换换行符(由附加引起):

sed '/%%TRACERT/{h;d;};G;s/\n/,/' file.csv

相关内容