This topic contains 6 replies, has 4 voices, and was last updated by
March 23, 2018 at 1:59 pm #96921
a csv file without attribute names is to be sorted with a primary key, and duplicates shall be removed.
In the following example the first column is the primary key (in another file it is the fourth column),
so in this example the fourth line (with ...secondname) is to be deleted:
ab123; name; first-name; ...
ab321; name2; first-name2; ...
ab123; name ; firstname-secondname; ...
Tried combinations of ...sort -property ... | sort -unique ... but I have too less experience with powershell.
Thanks in advance!
March 23, 2018 at 4:06 pm #96950ParticipantPoints: 0Rank: Member
to hide headers | format-table -hideheaders format-table -HideTableHeaders
March 23, 2018 at 6:54 pm #96960ParticipantPoints: 31Rank: Member
I think you need to import your CSV with the -Header option
#MyCSV file looks like this: #one,100,won #two,200,too #three,300,tree $header ="first","second","third" $file = import-csv -path .\MyCSV.csv -header $header #$file will look like this:first second third $file ----- ------ ----- one 100 won two 200 too three 300 tree #then you can sort the file contents on the named column $file | Sort -property third #your output will look like this: first second third ----- ------ ----- two 200 too three 300 tree one 100 won
March 23, 2018 at 6:54 pm #96963
March 27, 2018 at 2:30 pm #97197
Thank you very much!
But I misunderstand the cmdlet principle it seems. Whether I include the headers in the sqlcmd already,
or with the import, or with an extra header file like the following – the next step "sort"
returns one line only, the first data row, without header, and without error or warning.
powershell -command "(get-content accounts_?.out | Add-Content accounts_unsorted.out)"
powershell -command "get-content accounts_header.csv, accounts_unsorted.out | sort Key -unique | Set-Content accounts_%TIMESTAMP%.csv"
powershell -command "(get-content accounts_header.csv, accounts_unsorted.out | sort Key -unique) | Set-Content accounts_%TIMESTAMP%.csv"
Any help welcome!
March 27, 2018 at 3:46 pm #97203ParticipantPoints: 711Rank: Major Contributor
I don't know if I understood you right but if the question is how to properly import a CSV file without headers you could have just carefully reviewed the help for the cmdlet Import-CSV. Especially the example 5 shows how to do that. So it could be something like this.
Import-Csv -Path 'csvFile.csv' -Delimiter ',' -Header 'Header1','Header2' ... | Sort-Object -Property Header2 -Unique
April 6, 2018 at 3:13 pm #98121
Many thanks for the answers, and sorry for my poor English ;^)
Indeed I consulted the German PS Forum too (as uwmuc)
If I only had the time I'd search&read THE PS book...
The topic ‘sort combination of property and unique, for csv files without column names’ is closed to new replies.