第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

人到中年有點(diǎn)甜 2022-10-18 16:13:36
我需要一些幫助,所以我有一個(gè)包含以下信息的文件;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 個(gè)范圍,用于具有 IP 的新文件中的每個(gè)文件。預(yù)期成績(jī)。IP,Ports192.168.0.1 80,8980,6789,443,4778192.168.0.1 3556,7778,4432,5674,7786192.168.0.1 2234,6678,33245,7788,3332192.168.0.1 6678,3322,5432,5067192.168.0.2 80,8980,6789,443,4778192.168.0.2 3556,7778,4432,5674,7786192.168.0.2 2234,6678,33245,7788,3332192.168.0.2 6678,3322,5432,5067192.168.0.3 80,8980,6789,443,4778192.168.0.3 3556,7778,4432,5674,7786192.168.0.3 2234,6678,33245,7788,3332192.168.0.3 6678,3322,5432,5067192.168.0.4 80,8980,6789,443,4778192.168.0.4 3556,7778,4432,5674,7786192.168.0.4 2234,6678,33245,7788,3332192.168.0.4 6678,3322,5432,5067老實(shí)說(shuō),我不知道如何做到這一點(diǎn)或從哪里開始。請(qǐng)協(xié)助。無(wú)論是在 AWK 還是 python 中都可以,只要向我解釋一下腳本/單行代碼的作用,以便我可以嘗試使用它。
查看完整描述

2 回答

?
達(dá)令說(shuō)

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊

您能否嘗試以下操作(在顯示的示例中測(cè)試和編寫)。


awk -F'"|","' -v lines=$(wc -l < Input_file) '

BEGIN{

  print "IP,ports"

}

FNR>1{

  num=split($3,array," ")

  for(i=1;i<=num;i++){

    if(i==1){ printf $2 OFS }

    printf("%s%s",array[i],i%5==0||i==num?ORS:FNR==lines && i==num?ORS:",")

    if(i%5==0){ printf $2 OFS }

  }

}' Input_file

說(shuō)明:在此處添加上述的詳細(xì)說(shuō)明。


awk -F'"|","' -v lines=$(wc -l < Input_file) '                                  ##Starting awk program from here.

BEGIN{                                                                          ##Starting BEGIN section of this program.

  print "IP,ports"                                                              ##Printing headers here.

}

FNR>1{                                                                          ##Checking condition if current line number is greater than 1st line.

  num=split($3,array," ")                                                       ##Splitting 3rd field into an array with delimiter space.

  for(i=1;i<=num;i++){                                                          ##Traversing through all elements of array here.

    if(i==1){ printf $2 OFS }                                                   ##if its first element of array then print 2nd field of line and OFS.

    printf("%s%s",array[i],i%5==0||i==num?ORS:FNR==lines && i==num?ORS:",")     ##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.

    if(i%5==0){ printf $2 OFS }                                                 ##If its 5th element then print current line 2nd field with space

  }

}' Input_file                    


查看完整回答
反對(duì) 回復(fù) 2022-10-18
?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

對(duì)于 Python,您可以執(zhí)行以下操作:

演示:

from csv import DictReader, DictWriter


# Given attribute to https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks/312464#312464

def chunks(lst, n):

    """Yield successive n-sized chunks from lst."""

    for i in range(0, len(lst), n):

        yield lst[i:i + n]


# Open both input and output files

with open("data.csv") as f, open("output.csv", mode="w", newline='') as o:


    # Create reading and writing objects

    reader = DictReader(f)

    writer = DictWriter(o, fieldnames=["IP", "Ports"])


    # Write headers

    writer.writeheader()


    # Go through each line from reader object

    for line in reader:


        # Split ports by whitespace into a list of ports

        ports = line["Ports"].split()


        # Go through each chunk(n = 5) of ports

        for port_chunk in chunks(ports, 5):


            # Write row to output CSV file

            row_dict = {"IP": line["IP"], "Ports": ",".join(port_chunk)}

            writer.writerow(row_dict)

輸出.csv


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,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"


查看完整回答
反對(duì) 回復(fù) 2022-10-18
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)