我有两个CSV文件output.csv和Roster.csv
“ output.csv ”具有这些标头
UserID, User, Department, City, Group, Phone, Mobile, Extension, Office, Manager, Email
“ Roster.csv ”具有此标头
BUNID
我想将BUNID
与USERID
匹配并使用这些标头生成输出
UserID, User, Department, City, Group, Phone, Mobile, Extension, Office, Manager, Email, BUNID
问题是生成了输出,但BUNID字段是唯一空白的字段。
这是代码
$csv1 = Import-Csv "C:\CSVfiles\output.csv"
$csv2 = Import-csv "C:\CSVfiles\Roster.csv"
$Join = Join-Object -Left $csv1 -Right $csv2 -LeftJoinProperty UserID -RightJoinProperty BUNID -Type AllInLeft -RightProperties BUNID
$Join | select-object UserID, User, Department, City, Group, Phone, Mobile, Extension, Office, Manager, Email, BUNID | sort BUNID | Export-Csv "C:\CSVfiles\output_pas.csv"
在第三个CSV上输出这些字段。
UserID, User, Department, City, Group, Phone, Mobile, Extension, Office, Manager, Email, BUNID
输出应包含output.csv中的所有数据,并与Roster.csv中的BUNID匹配,类似于我们可以使用vlookup进行的操作。
我知道我犯了一个业余错误,但无法理解。
Output.csv示例
UserID,User,Department,City,Group,Phone,Mobile,Extension,Office,Manager,Email,
akumar58,Ankush,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520909,9811520909,256,//- (Sch E) 1,Aruna,dfg12@gtus.com,
akroy1,Roy,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520910,9811520910,257,//- (Sch E) 2,Aruna,dfg13@gtus.com,
kkhurana,"Khurana, Karan",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520911,9811520911,258,//- (Sch E) 3,Aruna,dfg14@gtus.com,
csharma1,"Sharma, Chetan",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520912,9811520912,259,//- (Sch E) 4,Chakra,dfg15@gtus.com,
sumit,Sumit,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520913,9811520913,260,//- (Sch E) 5,Chakra,dfg16@gtus.com,
saji,"Aji, Shiby",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520914,9811520914,261,//- (Sch E) 6,Chakra,dfg17@gtus.com,
rksharm1,"Sharma, Rajesh ",ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520915,9811520915,262,//- (Sch E) 7,Chakra,dfg18@gtus.com,
yxsingh,Singh,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520916,9811520916,263,//- (Sch E) 8,Aruna,dfg19@gtus.com,
sponnaga,Ponnaganti,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520917,9811520917,264,//- (Sch E) 9,Chakra,dfg20@gtus.com,
bmallena,Mallena,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520918,9811520918,265,//- (Sch E) 10,Aruna,dfg21@gtus.com,
ngarg,Garg,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520919,9811520919,266,//- (Sch E) 11,Chakra,dfg22@gtus.com,
dsharma5,Sharma,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520920,9811520920,267,//- (Sch E) 12,Aruna,dfg23@gtus.com,
rpyarwar,Yarwar,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520921,9811520921,268,//- (Sch E) 13,Chakra,dfg24@gtus.com,
hraj1,Hans,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520922,9811520922,269,//- (Sch E) 14,Chakra,dfg25@gtus.com,
Roster.CSV示例
BUNID
axsaxena
kjhebbar
smukher5
akroy1
kkhurana
csharma1
sumit
saji
rksharm1
yxsingh
bmallena
ngarg
dsharma5
rpyarwar
smohan1
hbmane
sdebnat1
skumar38
aprinja1
shanda
yhbijli1
bpannee1
saji
最终输出,我想获取什么,但是使用脚本时BUNID的最后一个字段为空。
UserID,User,Department,City,Group,Phone,Mobile,Extension,Office,Manager,Email,BUNID
akumar58,Ankush,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520909,9811520909,256,//- (Sch E) 1,Aruna,dfg12@gtus.com,
akroy1,Roy,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520910,9811520910,257,//- (Sch E) 2,Aruna,dfg13@gtus.com,akroy1
kkhurana,"Khurana, Karan",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520911,9811520911,258,//- (Sch E) 3,Aruna,dfg14@gtus.com,kkhurana
csharma1,"Sharma, Chetan",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520912,9811520912,259,//- (Sch E) 4,Chakra,dfg15@gtus.com,csharma1
sumit,Sumit,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520913,9811520913,260,//- (Sch E) 5,Chakra,dfg16@gtus.com,sumit
saji,"Aji, Shiby",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520914,9811520914,261,//- (Sch E) 6,Chakra,dfg17@gtus.com,saji
rksharm1,"Sharma, Rajesh ",ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520915,9811520915,262,//- (Sch E) 7,Chakra,dfg18@gtus.com,rksharm1
yxsingh,Singh,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520916,9811520916,263,//- (Sch E) 8,Aruna,dfg19@gtus.com,yxsingh
sponnaga,Ponnaganti,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520917,9811520917,264,//- (Sch E) 9,Chakra,dfg20@gtus.com,
bmallena,Mallena,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520918,9811520918,265,//- (Sch E) 10,Aruna,dfg21@gtus.com,bmallena
ngarg,Garg,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520919,9811520919,266,//- (Sch E) 11,Chakra,dfg22@gtus.com,ngarg
dsharma5,Sharma,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520920,9811520920,267,//- (Sch E) 12,Aruna,dfg23@gtus.com,dsharma5
rpyarwar,Yarwar,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520921,9811520921,268,//- (Sch E) 13,Chakra,dfg24@gtus.com,rpyarwar
hraj1,Hans,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520922,9811520922,269,//- (Sch E) 14,Chakra,dfg25@gtus.com,
答案 0 :(得分:3)
这是合并两个CSV文件的一种方法。它不会进行传统的合并,因为它会添加$ Roster条目,而不是将其与$ UserID条目合并。
# fake reading in a CSV file
# in real life, use Import-CSV
$Roster = @'
BUNID
axsaxena
kjhebbar
smukher5
akroy1
kkhurana
csharma1
sumit
saji
rksharm1
yxsingh
bmallena
ngarg
dsharma5
rpyarwar
smohan1
hbmane
sdebnat1
skumar38
aprinja1
shanda
yhbijli1
bpannee1
saji
'@ | ConvertFrom-Csv
# fake reading in another CSV file
# apparent accidental trailing comma manually removed from each line
$OutputCSV = @'
UserID,User,Department,City,Group,Phone,Mobile,Extension,Office,Manager,Email
akumar58,Ankush,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520909,9811520909,256,//- (Sch E) 1,Aruna,dfg12@gtus.com
akroy1,Roy,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520910,9811520910,257,//- (Sch E) 2,Aruna,dfg13@gtus.com
kkhurana,"Khurana, Karan",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520911,9811520911,258,//- (Sch E) 3,Aruna,dfg14@gtus.com
csharma1,"Sharma, Chetan",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520912,9811520912,259,//- (Sch E) 4,Chakra,dfg15@gtus.com
sumit,Sumit,DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520913,9811520913,260,//- (Sch E) 5,Chakra,dfg16@gtus.com
saji,"Aji, Shiby",DESIGNERS,ALEXANDRIA,VOS-BPCS_Elec,9811520914,9811520914,261,//- (Sch E) 6,Chakra,dfg17@gtus.com
rksharm1,"Sharma, Rajesh ",ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520915,9811520915,262,//- (Sch E) 7,Chakra,dfg18@gtus.com
yxsingh,Singh,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520916,9811520916,263,//- (Sch E) 8,Aruna,dfg19@gtus.com
sponnaga,Ponnaganti,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520917,9811520917,264,//- (Sch E) 9,Chakra,dfg20@gtus.com
bmallena,Mallena,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520918,9811520918,265,//- (Sch E) 10,Aruna,dfg21@gtus.com
ngarg,Garg,ELECTRICAL PR,ALEXANDRIA,VOS-BPCS_Elec,9811520919,9811520919,266,//- (Sch E) 11,Chakra,dfg22@gtus.com
dsharma5,Sharma,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520920,9811520920,267,//- (Sch E) 12,Aruna,dfg23@gtus.com
rpyarwar,Yarwar,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520921,9811520921,268,//- (Sch E) 13,Chakra,dfg24@gtus.com
hraj1,Hans,PDP,ALEXANDRIA,VOS-BPCS_Elec,9811520922,9811520922,269,//- (Sch E) 14,Chakra,dfg25@gtus.com
'@ | ConvertFrom-Csv
# if you want this to be blank or $Null when no match is found,
# replace the below with the desired value or "$Null"
$NotFound = '__NotFound__'
$Results = foreach ($OC_Item in $OutputCSV)
{
if ($OC_Item.UserID -in $Roster.BUNID)
{
$BUNID = $OC_Item.UserID
}
else
{
$BUNID = $NotFound
}
$OC_Item | Add-Member -MemberType NoteProperty -Name 'BUNID' -Value $BUNID
$OC_Item
}
$Results = $Results |
Sort-Object -Property BUNID
# send to screen
$Results
# send to CSV file
$Results |
Export-Csv -LiteralPath "$env:TEMP\Magenoob_-_Merged_User_Info.csv" -NoTypeInformation
在屏幕输出上被截断...
UserID : akumar58
User : Ankush
Department : DESIGNERS
City : ALEXANDRIA
Group : VOS-BPCS_Elec
Phone : 9811520909
Mobile : 9811520909
Extension : 256
Office : //- (Sch E) 1
Manager : Aruna
Email : dfg12@gtus.com
BUNID : __NotFound__
[*...snip...*]
UserID : yxsingh
User : Singh
Department : ELECTRICAL PR
City : ALEXANDRIA
Group : VOS-BPCS_Elec
Phone : 9811520916
Mobile : 9811520916
Extension : 263
Office : //- (Sch E) 8
Manager : Aruna
Email : dfg19@gtus.com
BUNID : yxsingh
截断的CSV文件内容...
"UserID","User","Department","City","Group","Phone","Mobile","Extension","Office","Manager","Email","BUNID"
"akumar58","Ankush","DESIGNERS","ALEXANDRIA","VOS-BPCS_Elec","9811520909","9811520909","256","//- (Sch E) 1","Aruna","dfg12@gtus.com","__NotFound__"
[*...snip...*]
"yxsingh","Singh","ELECTRICAL PR","ALEXANDRIA","VOS-BPCS_Elec","9811520916","9811520916","263","//- (Sch E) 8","Aruna","dfg19@gtus.com","yxsingh"
答案 1 :(得分:1)
IIUR,如果UserID与roster.csv中的BUNID相匹配,您只想附加字段BUNID
,
因此,在计算的属性中包含Select-Object
且包含@Lee_Dailey if
的单个管道应该执行以下操作:
$Roster = Import-Csv "C:\CSVfiles\Roster.csv"
Import-Csv "C:\CSVfiles\output.csv" | Select-Object *,
@{n='BUNID';e={if($_.UserID -in $Roster.BUNID){$_.UserID}else{"__NotFound__"}}} |
Sort-Object BUNID | Export-Csv "C:\CSVfiles\output_pas.csv" -NoTypeInformation
如果新文件应不包含Roster.csv中不匹配的行:
$Roster = Import-Csv "C:\CSVfiles\Roster.csv"
Import-Csv "C:\CSVfiles\output.csv" | Where-Object UserID -in $Roster.BUNID |
Select-Object *,@{n='BUNID';e={$_.UserID}} |
Sort-Object BUNID | Export-Csv "C:\CSVfiles\output_pas.csv" -NoTypeInformation