我有两个文件,我需要根据三列的值加入它们,如果不匹配则在不匹配的列中打印NA。
enter code here
$("#getFile").html('');
$("#getFile").html('<video src="address of the uploaded-folder on server" controls></video>');
目前我输出的输出不正确:
cat f1
AAA 0 node4 Activated Unreachable down
AAA 1 node3 Activated Pingable cool
cat f2
AAA 0 node3 XYZ Active
期望的输出:
awk 'NR==FNR{a[$1]=$1;b[$2]=$2;c[$3]=$3;next} $1 in a && $2 in b && $3 in c{print $0}' f1 f2
AAA 0 node3 XYZ Active
答案 0 :(得分:1)
使用Awk
逻辑如下,
awk 'FNR==NR{hash[$1FS$3]=$NF; next}{for(i in hash) if (match(i,$1FS$3)) { $(NF+1)=hash[i] } else { $(NF+1)="NA" } }1' f2 f1
根据需要生成输出。
AAA 0 node4 Activated Unreachable down NA
AAA 1 node3 Activated Pingable cool Active
想法是先解析第二个文件以存储状态,将节点值索引到数组hash
中。然后在第一个文件上,所有索引上的循环,如果$3
上的f1
中的值与散列值匹配,则相应地打印状态,而不是仅仅打印{ {1}}。
答案 1 :(得分:1)
awk 方法:
awk 'NR==FNR{a[$1,$3]=$5; next}{$7="NA";if(($1,$3) in a){$7=a[$1,$3]} print}' f2 f1
输出:
AAA 0 node4 Activated Unreachable down NA
AAA 1 node3 Activated Pingable cool Active
a[$1,$3]=$5
- 使用第一个$5
和第三个f2
字段的组合作为数组键,将第五个字段$1
的值存储在第二个文件$3
中
$7="NA";
- 启动额外的第七个字段$7
,默认值为“NA”