文本处理 - 基于两个不同的文件进行修改

文本处理 - 基于两个不同的文件进行修改

我有两个名为 one.txt 的文件,其中包含

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

和另外两个.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.11),则第 5 个字段应替换为 Two.txt 中的第二个字段,并且第 6 个字段应替换为 Two.txt 中的第三个字段。修改的任何行还应将第 3 个字段替换为19,第 7 个字段替换为255.255.255.0,第 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"

相关内容