config
service1,208000,规则1
service2,308000,rule2
文件1
值1,值2,服务1 | servicex |服务2,值3,value4,208000
值1,值2,服务1,值3,value4,208000
值1,值2,服务1,值3,值4,
值1,值,servicex,值3,value4,208000
值1,值,服务2,值3,value4,308000
值1,值,服务2,值3,value4,308000
value1,value2,service3,value3,value4,408000
输出
值1,值2,服务1 | servicex |服务2,值3,value4,208000,规则1
值1,值2,服务1,值3,value4,208000,规则1
值1,值2,服务1,值3,值4,NORULEMATCHING
值1,值,servicex,值3,value4,208000,NORULEMATCHING
值1,值,服务2,值3,value4,308000,规则2
值1,值,服务2,值3,value4,308000,规则2
value1,value2,service3,value3,value4,408000,NORULEMATCHING
我试过以下内容:
awk -f myawk.awk config file1
其中myawk.awk
是
BEGIN { FS = OFS = "," }
FNR == NR {
servicename=$1;
qos=$2;
$1 = "";
$2 = "";
a[servicename,qos] = $0;
next
}
{
split($3,plan,"|");
if(plan[1]","$6 in a){
split(a[plan[1]","$6],ruledesc,",");
$7 = ruledesc[3];
print $0;
}
else {
$7 = NORULEMATCHING;
print $0;
}
}
注意:
- 从配置文件中,我们需要匹配file1的column3(作为管道的FD之后的第一个字段)和第5列的column1和第2列(组合为主键)
- 如果匹配,则在输出
我面临的挑战是在2列的基础上创建一个数组。
答案 0 :(得分:1)
您可以尝试:
awk -f myawk.awk config file1
其中myawk.awk
是:
BEGIN { FS = OFS = "," }
FNR == NR {
a[$1,$2] = $3
next
}
{
split($3,plan,"|")
key=plan[1] SUBSEP $6
if( key in a){
$7 = a[key]
print $0
} else {
$7 = "NORULEMATCHING"
print $0
}
}