Asked by:
Powershell to delete the records from a file based on duplicate field of 1st column

Question
-
Hi,
I am looking for a powershell script which will delete the records if the first field is a duplicate
Example:-
A1,X1,100
A1,X2,200
B1,Y1,300
C1,Z1,400
C1,Z2,500
C3,Z3,600
expected output
A1,X1,100
B1,Y1,300
C1,Z1,400
Thanks
Wednesday, July 25, 2018 2:31 PM
All replies
-
Use Select-Object field1 -Unique
\_(ツ)_/
Wednesday, July 25, 2018 2:56 PM -
$csv = @' Field1,Field2,Field3 A1,X1,100 A1,X2,200 B1,Y1,300 C1,Z1,400 C1,Z2,500 C3,Z3,600 '@ $csv | ConvertFrom-Csv | group Field1 | foreach { $_.Group | select -first 1 }
Wednesday, July 25, 2018 3:21 PM -
Hi,
Thanks for the reply. but, it is actually a pipe ("|") delimited and there is no header in the file.
Thanks
Wednesday, July 25, 2018 3:52 PM -
Import-Csv does that.
See:
help Import-Csv -online
\_(ツ)_/
Wednesday, July 25, 2018 3:53 PM -
will the import-Csv work for text file with pipe delimited? ;)
Thanks
Wednesday, July 25, 2018 4:20 PM -
Don't be lazy. Look it up.Wednesday, July 25, 2018 4:23 PM
-
Don't be lazy. Look it up.
Are you afraid to try it? I assure you it won't break anything.
Look at the examples. They give a complete answer to your questions.
\_(ツ)_/
Wednesday, July 25, 2018 4:36 PM -
I tried by referring some example and I am getting some error. Not sure what the error is .
Get-Content .\file1.txt |
Import-Csv -Header "Column1", "Column2", "Column3" |
Sort -Unique -Property Column1 |
% {"{1},{2},{3}" -f $_.Column1, $_.Column2, $_.Column3} |
Set-Content file2.txt
Import-Csv : You must specify either the -Path or -LiteralPath parameters, but not both.
At line:2 char:1
+ Import-Csv -Header "Column1", "Column2", "Column3" |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Import-Csv], InvalidOperationException
+ FullyQualifiedErrorId : CannotSpecifyPathAndLiteralPath,Microsoft.PowerShell.Commands.ImportCsvCommand
Wednesday, July 25, 2018 5:03 PM -
Have a closer look at example #2.Wednesday, July 25, 2018 5:14 PM