问题描述
我有两个csv文件,我想要比较这两个文件,并找到difference.It包含user_id
我试图与compare-object,diff但无法实现。
喜欢
AD_Users.csv
Oracle_Users .csv
都包含用户ID,输出应该像
AD用户不存在于Oracle
Oracle用户在AD中不存在
例如 - K9988484
J8485888
我尝试使用compare-object,diff但是无法实现。
看起来像这样:
#的内容ad.csv
user_id,field1,field2
useronlyad,value1 ,value2
userboth,value3,value4
而且:
#content of oracle.csv
user_id,field1,field2
useronlyoracle,value1,value2
userboth,value3,value4
您可以获得不是oracle用户喜欢的广告用户(借用):
#返回所有不是oracle用户的广告用户
类似的,你可以得到不是这样的广告用户的oracle用户
import-module .\Scripting.psm1
$ ad_hashset = new-hashset string
$ oracle_hashset = new-hashset string
import-csv .\ad.csv | ForEach-Object {$ ad_hashset.add($ _。user_id)}
import-csv .\oracle.csv | ForEach-Object {$ oracle_hashset.add($ _。user_id)}
$ ad_hashset.ExceptWith($ oracle_hashset)
$ ad_hashset#将返回useronlyad
code>#返回所有不是oracle用户的广告用户
import-module .\Scripting.psm1
$ ad_hashset = new-hashset string
$ oracle_hashset = new-hashset string
import-csv .\ad.csv | ForEach-Object {$ ad_hashset.add($ _。user_id)}
import-csv .\oracle.csv | ForEach-Object {$ oracle_hashset.add($ _。user_id)}
$ oracle_hashset.ExceptWith($ ad_hashset)
$ oracle_hashset#将返回useronlyoracle
pre>
简要说明:
- 创建两个集合(散列集)
- 使用在其对应的CSV中找到的数据填充散列集
- 删除第二个集合中的所有元素第一个收藏
I have two csv files , I want to compare both files and find difference.It contains user_id
I tried with compare-object, diff but could not achieve.
like
AD_Users.csv
Oracle_Users.csv
both contains user id , output should be like
AD user does not exist in Oracle
Oracle user does not exist in AD
eg - K9988484 J8485888
I tried with compare-object, diff but could not achieve.
解决方案Assuming your csv files look like this:
# contents of ad.csv user_id,field1,field2 useronlyad,value1,value2 userboth,value3,value4
and this:
# contents of oracle.csv user_id,field1,field2 useronlyoracle,value1,value2 userboth,value3,value4
You could get the ad users that are not oracle users like this (borrowing New-HashSet from Josh Einstein):
# to return all ad users that are not oracle users import-module .\Scripting.psm1 $ad_hashset = new-hashset string $oracle_hashset = new-hashset string import-csv .\ad.csv | ForEach-Object {$ad_hashset.add($_.user_id)} import-csv .\oracle.csv | ForEach-Object {$oracle_hashset.add($_.user_id)} $ad_hashset.ExceptWith($oracle_hashset) $ad_hashset # will return useronlyad
Similarly, you could get the oracle users that are not ad users like this
# to return all ad users that are not oracle users import-module .\Scripting.psm1 $ad_hashset = new-hashset string $oracle_hashset = new-hashset string import-csv .\ad.csv | ForEach-Object {$ad_hashset.add($_.user_id)} import-csv .\oracle.csv | ForEach-Object {$oracle_hashset.add($_.user_id)} $oracle_hashset.ExceptWith($ad_hashset) $oracle_hashset # will return useronlyoracle
Brief explanation of how this works:
- Create two collections (hashsets)
- Populate the hashsets with the data found in their corresponding CSV
- Removes all elements in the second collection from the first collection
这篇关于如何比较两个csv文件,并使用powershell找到区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!