PowerShell以CSV格式

时间:2017-11-13 03:32:02

标签: powershell csv

我有一个像这样的标题的csv

ID,名称,IP,详细

有一些价值会像这样重复

1,John,192.168.1.1,Details1
2,Mary,192.168.1.2,Details2
3,John,192.168.1.3,Details3
4,Dick,192.168.1.1,Details4
5,Kent,192.168.1.4,Details5

无论如何,我可以选择所有具有重复值的行吗?

期望的输出:

1,John,192.168.1.1,Details1
3,John,192.168.1.3,Details3
4,Dick,192.168.1.1,Details4

到目前为止,我已经尝试了

Import-csv file | group | sort -des | select -f 10

但结果只对那些整行匹配

的人进行分组

如果有人能伸出援助之手或指示我解决这个问题的方向,我将不胜感激。在此先感谢您的回复

1 个答案:

答案 0 :(得分:0)

I'm not sure if I've understood your problem up to 100%, but as far as I've understood you want something like this:

# ID Name IP          Detail  
#-- ---- --          ------  
#1  John 192.168.1.1 Details1
#2  Mary 192.168.1.2 Details2
#3  John 192.168.1.3 Details3
#4  Dick 192.168.1.1 Details4
#5  Kent 192.168.1.4 Details5

$input = Import-csv .\input.csv 
$input

$uniqueNames = $input.Name | select -Unique
$duplicateNames = Compare-Object -ReferenceObject $input.Name -DifferenceObject $uniqueNames | ? { $_.SideIndicator -like "<=" } | select -ExpandProperty InputObject


$uniqueIps = $input.IP | select -Unique
$duplicateIps = Compare-Object -ReferenceObject $input.IP -DifferenceObject $uniqueIps | ? { $_.SideIndicator -like "<=" } | select -ExpandProperty InputObject

Write-Output ""
Write-Output "========================="

Write-Output "Duplicate Names: $duplicateNames"
Write-Output "Duplicate IPs: $duplicateIps"

Output:

ID Name IP          Detail  
-- ---- --          ------  
1  John 192.168.1.1 Details1
2  Mary 192.168.1.2 Details2
3  John 192.168.1.3 Details3
4  Dick 192.168.1.1 Details4
5  Kent 192.168.1.4 Details5

=========================
Duplicate Names: John
Duplicate Names: 192.168.1.1

Hope that helps.