【发布时间】:2020-03-09 20:08:05
【问题描述】:
我有一个输入 CSV,我想将其拆分为两个 CSV 文件。如果第 4 列的值与 WLTarray 中的任何值匹配,则它应该进入输出文件 1,如果不匹配,则应该进入输出文件 2。
WLT 数组:
"22532" "79994" "18809" "21032"
输入 CSV 文件:
header1,header2,header3,header4,header5,header6,header7,header8
"83","6344324","585677","22532","Entitlements","BX","22532:718","36721"
"83","1223432","616454","79994","Compliance Stuff","DR","79994:64703","206134"
"83","162217","616454","83223","Data Enrichment","IEO","83223:64701","206475"
"83","267216","616457","79994","Compliance Engine","ABC","79994:64703","206020"
输出 CSV 文件 1:
header1,header2,header3,header4,header5,header6,header7,header8
"83","6344324","585677","22532","Entitlements","BX","22532:718","36721"
"83","1223432","616454","79994","Compliance Stuff","DR","79994:64703","206134"
"83","267216","616457","79994","Compliance Engine","ABC","79994:64703","206020"
输出 CSV 文件 2:
header1,header2,header3,header4,header5,header6,header7,header8
"83","162217","616454","83223","Data Enrichment","IEO","83223:64701","206475"
我一直在寻找 awk 来过滤它(python 和 perl 在我的环境中不是一个选项),但我认为可能有更聪明的方法:
declare -a WLTarray=("22532" "79994" "18809" "21032")
for WLTvalue in "${WLTarray[@]}" #Everything in the WLTarray will go to $filename-WLT.tmp
do
awk -F, '($4=='$WLTvalue'){print}' $filename.tmp >> $filename-WLT.tmp #move the lines to the WLT file
# now filter to remove non matching values? why not just move the rows entirely?
done
【问题讨论】: