使用传递的数组中的元素将数组传递给awk中的awk + ​​if语句

时间:2016-07-22 04:16:30

标签: arrays bash awk

这是我的档案:

$ cat head_datafile_pipe_deleimiter.csv
"Rec_Open_Date"|"MSISDN"|"IMEI"|"Data_Volume_Bytes"|"Device_Manufacturer"|"Device_Model"|"Product_Description"|"Data_Volume_MB"
"2016-07-17"|"686"|"630"|"618320"|"Apple Inc"|"Apple iPhone S A1530"|"PREPAY PLUS - $0 -"|"0.589676"
"2016-07-17"|"560"|"570"|"42841779"|"Motorola Mobility LLC, a Lenovo Company"|"Moto X 2nd Generation, X112360445"|"$39.95 Plan"|"40.8571"
"2016-07-17"|"811"|"340"|"2465082"|"Samsung Korea"|"Samsung SM-G900I"|"$69.95 Plan"|"2.35089"
"2016-07-17"|"785"|"610"|"41498628"|"Apple Inc"|"Apple iPhone 6S Plus A1687"|"$29.95 Carryover Plan 1GB"|"39.5762"
"2016-07-17"|"908"|"310"|"6497563"|"Samsung Korea"|"Samsung GT-I9195"|"PREPAY PLUS - $0 -"|"6.19656"
"2016-07-17"|"919"|"610"|"0"|"Samsung Korea"|"Samsung SM-G925I"|"$19 CO COMBO - NOT RECURRENT"|"0"
"2016-07-17"|"356"|"290"|"33189681"|"Apple Inc"|"Apple iPhone 6S A1688"|"$39.95 Plan"|"31.6521"
"2016-07-17"|"009"|"160"|"30340"|"Samsung Korea"|"Samsung SM-J500Y"|"PREPAY PLUS - $1 - #33"|"0.0289345"
"2016-07-17"|"574"|"400"|"549067"|"HUAWEI Technologies Co Ltd"|"HUAWEI Y6"|"PREPAY PLUS - $0 -"|"0.523631"

在这里我接近我想要的但我不能让它迭代遍历数组中的不同元素。 if($7==dq"PREPAY PLUS - $0 -"dq) print $7。它基本上是一个for循环,它遍历数组中的元素,并且在移动到数组中的下一个元素并重复之前过滤掉我想要的值。但是,如果我对值进行硬编码,我只能让它工作,我希望它遍历数组u_vals

$ for i in "${u_vals[@]}"; do awk -F'|' -v var="${u_vals[*]}" -v j=i -v dq='"' 'NR==1{print $7} NR>1{split(var,list,"|"); if($7==dq"PREPAY PLUS - $0 -"dq) print $7 }' head_datafile_pipe_deleimiter.csv; done
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"

这些都是第7列中的uniq值

$ printf "%s\n" "${u_vals[@]}"
"$19 CO COMBO - NOT RECURRENT"
"$29.95 Carryover Plan 1GB"
"$39.95 Plan"
"$69.95 Plan"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $1 - #33"

如何制作此部分
dq"PREPAY PLUS - $0 -"dq
这个
if($7==dq"PREPAY PLUS - $0 -"dq) print $7
遍历数组的元素?

EDIT1

这就是我想要的:

awk -F“|” ...

$ for i in "${u_vals[@]}"; do awk -F"|" -v j="$i"  'NR==1{print $7}NR>1 {if($7==j) print $7 }' head_datafile_pipe_deleimiter.csv ; done
"Product_Description"
"$19 CO COMBO - NOT RECURRENT"
"Product_Description"
"$29.95 Carryover Plan 1GB"
"Product_Description"
"$39.95 Plan"
"$39.95 Plan"
"Product_Description"
"$69.95 Plan"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $1 - #33"

awk -v FS =“|” ....

$ for i in "${u_vals[@]}"; do awk -v FS="|" -v j="$i"  'NR==1{print $7}NR>1 {if($7==j) print $7 }' head_datafile_pipe_deleimiter.csv ; done
"Product_Description"
"$19 CO COMBO - NOT RECURRENT"
"Product_Description"
"$29.95 Carryover Plan 1GB"
"Product_Description"
"$39.95 Plan"
"$39.95 Plan"
"Product_Description"
"$69.95 Plan"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $1 - #33"

1 个答案:

答案 0 :(得分:1)

你的语法错误很小。当你在awk中时

awk ... -v j=i

应该是

awk ... -v j="$i"

这应该有效:

for i in "${u_vals[@]}"; do awk -v FS="|" -v j="$i"  'NR==1{print $7}NR>1 {if($7==j) print $7 }' datafile_pipe_deleimiter.csv ; done

awk的以下部分:

split(var,list,"|");

我不知道你为什么需要它,所以我没有提出我的答案