テキスト処理 - 2つの異なるファイルに基づく修正

テキスト処理 - 2つの異なるファイルに基づく修正

one.txtという2つのファイルがあります。

"297","237","14",NULL,"51.195.14.201","02:00:00:1a:1c:06","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0"
"303","242","14",NULL,"51.195.14.207","02:00:00:a3:03:10","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0"
"323","249","14",NULL,"51.195.14.209","02:00:00:dd:1f:11","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0"

そして、以下を含む他の2つの.txt

51.195.14.201, 193.138.35.127, 02:00:00:20:a2:27
51.195.14.209, 193.138.35.128, 02:00:00:20:a2:28

Two.txtの最初のフィールドにone.txtのフィールド5と同じ入力を含む行が含まれている場合(正確でなければならず、例えば衝突する場合51.195.14.1)、51.195.14.115番目のフィールドはTwoの2番目のフィールドに置き換える。 .txt、6番目のフィールドはTwo.txtの3番目のフィールドに置き換える必要があります。変更されたすべての行には、3番目のフィールド19がに変わり255.255.255.0、7番目のフィールドがに変わり、8番目のフィールドがに変わり193.138.35.1、9番目のフィールドがに変わります。24

変更は次のようにする必要があります

"297","237","19",NULL,"193.138.35.127","02:00:00:20:a2:27","255.255.255.0","193.138.35.1","24",NULL,NULL,"net0"
"303","242","14",NULL,"51.195.14.207","02:00:00:a3:03:10","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0"
"323","249","19",NULL,"193.138.35.128","02:00:00:20:a2:28","255.255.255.0","193.138.35.1","24",NULL,NULL,"net0"

一部の列にはNULL引用符が含まれていません。

答え1

awkを使用してください。

$ awk -F', *' -v OFS=',' -v q='"' '
    NR==FNR {a[q $1 q]=$2; b[q $1 q]=$3; next }
    $5 in a {
        $6=q b[$5] q; $5=q a[$5] q; $3=q 19 q; $7=q "255.255.255.0" q;
        $8=q "193.138.35.1" q; $9=q 24 q
    }
    { print }
' two.txt one.txt
"297","237","19",NULL,"193.138.35.127","02:00:00:20:a2:27","255.255.255.0","193.138.35.1","24",NULL,NULL,"net0"
"303","242","14",NULL,"51.195.14.207","02:00:00:a3:03:10","255.0.0.0","10.0.10.1","32",NULL,NULL,"net0"
"323","249","19",NULL,"193.138.35.128","02:00:00:20:a2:28","255.255.255.0","193.138.35.1","24",NULL,NULL,"net0"

関連情報