使用 IP 在新系列中拆分端口范围

我需要一些帮助,所以我有一个包含以下信息的文件;


IP,Ports,count

"192.168.0.1","80 8980 6789 443 4778 3556 7778 4432 5674 7786 2234 6678 33245 7788 3332 6678 3322 5432 5567",19

"192.168.0.2","80 8980 6789 443 4778 3556 7778 4432 5674 7786 2234 6678 33245 7788 3332 6678 3322 5432 5567",19

"192.168.0.3","80 8980 6789 443 4778 3556 7778 4432 5674 7786 2234 6678 33245 7788 3332 6678 3322 5432 5567",19

"192.168.0.4","80 8980 6789 443 4778 3556 7778 4432 5674 7786 2234 6678 33245 7788 3332 6678 3322 5432 5567",19

我想将端口拆分为 5 个范围,用于具有 IP 的新文件中的每个文件。


预期成绩。


IP,Ports

192.168.0.1 80,8980,6789,443,4778

192.168.0.1 3556,7778,4432,5674,7786

192.168.0.1 2234,6678,33245,7788,3332

192.168.0.1 6678,3322,5432,5067

192.168.0.2 80,8980,6789,443,4778

192.168.0.2 3556,7778,4432,5674,7786

192.168.0.2 2234,6678,33245,7788,3332

192.168.0.2 6678,3322,5432,5067

192.168.0.3 80,8980,6789,443,4778

192.168.0.3 3556,7778,4432,5674,7786

192.168.0.3 2234,6678,33245,7788,3332

192.168.0.3 6678,3322,5432,5067

192.168.0.4 80,8980,6789,443,4778

192.168.0.4 3556,7778,4432,5674,7786

192.168.0.4 2234,6678,33245,7788,3332

192.168.0.4 6678,3322,5432,5067

老实说,我不知道如何做到这一点或从哪里开始。请协助。


无论是在 AWK 还是 python 中都可以,只要向我解释一下脚本/单行代码的作用,以便我可以尝试使用它。


人到中年有点甜
浏览 156回答 2
2回答

达令说

您能否尝试以下操作(在显示的示例中测试和编写)。awk -F'"|","' -v lines=$(wc -l < Input_file) 'BEGIN{&nbsp; print "IP,ports"}FNR>1{&nbsp; num=split($3,array," ")&nbsp; for(i=1;i<=num;i++){&nbsp; &nbsp; if(i==1){ printf $2 OFS }&nbsp; &nbsp; printf("%s%s",array[i],i%5==0||i==num?ORS:FNR==lines && i==num?ORS:",")&nbsp; &nbsp; if(i%5==0){ printf $2 OFS }&nbsp; }}' Input_file说明:在此处添加上述的详细说明。awk -F'"|","' -v lines=$(wc -l < Input_file) '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Starting awk program from here.BEGIN{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Starting BEGIN section of this program.&nbsp; print "IP,ports"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Printing headers here.}FNR>1{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Checking condition if current line number is greater than 1st line.&nbsp; num=split($3,array," ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Splitting 3rd field into an array with delimiter space.&nbsp; for(i=1;i<=num;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Traversing through all elements of array here.&nbsp; &nbsp; if(i==1){ printf $2 OFS }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##if its first element of array then print 2nd field of line and OFS.&nbsp; &nbsp; printf("%s%s",array[i],i%5==0||i==num?ORS:FNR==lines && i==num?ORS:",")&nbsp; &nbsp; &nbsp;##Printing array value along with condition if its 5 element or number of total elements equals i then print new line OR current line number equal to lines OR i equals to num then print new line OR print comma.&nbsp; &nbsp; if(i%5==0){ printf $2 OFS }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##If its 5th element then print current line 2nd field with space&nbsp; }}' Input_file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

扬帆大鱼

对于 Python,您可以执行以下操作:使用 读取 CSV 文件csv.DictReader并使用 . 打开要写入的文件DictReader。DictReader将每一行映射到 a&nbsp;dict,这样可以更轻松地访问 CSV 列。使用 .写入输出 CSV 标头DictWriter.writeheader。使用循环迭代文件的每一行for。使用 . 分割空格上的端口str.split。每 5 个端口分块。我们可以使用如何将列表拆分为大小均匀的块中的方法?&nbsp;.遍历这些块中的每一个,然后使用csvwriter.writerow.&nbsp;我们确保只包括IP和Ports列。演示:from csv import DictReader, DictWriter# Given attribute to https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks/312464#312464def chunks(lst, n):&nbsp; &nbsp; """Yield successive n-sized chunks from lst."""&nbsp; &nbsp; for i in range(0, len(lst), n):&nbsp; &nbsp; &nbsp; &nbsp; yield lst[i:i + n]# Open both input and output fileswith open("data.csv") as f, open("output.csv", mode="w", newline='') as o:&nbsp; &nbsp; # Create reading and writing objects&nbsp; &nbsp; reader = DictReader(f)&nbsp; &nbsp; writer = DictWriter(o, fieldnames=["IP", "Ports"])&nbsp; &nbsp; # Write headers&nbsp; &nbsp; writer.writeheader()&nbsp; &nbsp; # Go through each line from reader object&nbsp; &nbsp; for line in reader:&nbsp; &nbsp; &nbsp; &nbsp; # Split ports by whitespace into a list of ports&nbsp; &nbsp; &nbsp; &nbsp; ports = line["Ports"].split()&nbsp; &nbsp; &nbsp; &nbsp; # Go through each chunk(n = 5) of ports&nbsp; &nbsp; &nbsp; &nbsp; for port_chunk in chunks(ports, 5):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Write row to output CSV file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row_dict = {"IP": line["IP"], "Ports": ",".join(port_chunk)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.writerow(row_dict)输出.csvIP,Ports192.168.0.1,"80,8980,6789,443,4778"192.168.0.1,"3556,7778,4432,5674,7786"192.168.0.1,"2234,6678,33245,7788,3332"192.168.0.1,"6678,3322,5432,5567"192.168.0.2,"80,8980,6789,443,4778"192.168.0.2,"3556,7778,4432,5674,7786"192.168.0.2,"2234,6678,33245,7788,3332"192.168.0.2,"6678,3322,5432,5567"192.168.0.3,"80,8980,6789,443,4778"192.168.0.3,"3556,7778,4432,5674,7786"192.168.0.3,"2234,6678,33245,7788,3332"192.168.0.3,"6678,3322,5432,5567"192.168.0.4,"80,8980,6789,443,4778"192.168.0.4,"3556,7778,4432,5674,7786"192.168.0.4,"2234,6678,33245,7788,3332"192.168.0.4,"6678,3322,5432,5567"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python