我有一個 csv,我想讓 PowerShell 為名為“art”的列中的每個唯一值提取 10 行。在確定了獨特的藝術價值之后,我需要為這些價值提供單獨的 csv 檔案。我撰寫的代碼對我不起作用。
If (Test-Path D:\WORK\JetLetter\TPL\TPL_SNAP\data\TPL_Mailer.csv){
$source=Get-ChildItem "D:\WORK\JetLetter\TPL\TPL_SNAP\data\TPL_Mailer.csv" | select -Last 1
$csvFile = 'D:\WORK\JetLetter\TPL\TPL_SNAP\data\TPL_Mailer.csv'
$arttag = Import-Csv $csvFile | Group-Object | Select-Object "art" -Force
Export-csv $arttag
}
我得到的錯誤是:
Select-Object : A parameter cannot be found that matches parameter name 'Force'.
At line:5 char:62
$arttag = Import-Csv $csvFile | Group-Object | Select-Object -Force
~~~~~~
CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjec
tCommand
Export-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
At line:6 char:12
Export-csv $arttag
~~~~~~~
CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationExcepti
on
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.E
xportCsvCommand
匯入的 csv 將包含多行名稱、地址、藝術標簽和序列號。
sequence art last first address city st zip
1 3S Doe John 123 Any Street AnyTown CA 12345
11 OG Dow Jane 124 Any Street AnyTown CA 12346
21 OGF Cool Joe 125 Any Street AnyTown CA 12347
31 SLV Cool Carol 126 Any Street AnyTown CA 12348
41 SMP Bravo Johnny 127 Any Street AnyTown CA 12349
無論資料中的現有列是什么,我都需要匯出一個包含 10 行每個藝術值的 csv。
任何幫助將不勝感激。
我修改了腳本以使其更簡單,但是我沒有看到我需要的結果。
$source=Get-ChildItem 'D:\WORK\JetLetter\TPL\TPL_SNAP\data\TPL_Mailer.csv' | select -Last 1
$csvFile = 'D:\WORK\JetLetter\TPL\TPL_SNAP\data\TPL_Mailer.csv'
import-csv $csvFile | Select-Object -Property 'art' -Unique | export-csv 'D:\WORK\JetLetter\TPL\TPL_SNAP\data\art_proofs.csv' -NoTypeInformation
我得到的是:
art
APS
DWS
我需要每一列都有每個藝術行的標題。我正在尋找的是一種按藝術標簽過濾并匯出過濾結果的方法。匯入的 csv 可以有 1000 到 400,000 條記錄。例如:
sequence art last first address city st zip
1 3S Doe John 123 Any Street AnyTown CA 12345
2 OG Dow Jane 124 Any Street AnyTown CA 12346
3 OGF Cool Joe 125 Any Street AnyTown CA 12347
4 SLV Cool Carol 126 Any Street AnyTown CA 12348
5 SMP Bravo Johnny 127 Any Street AnyTown CA 12349
6 3S Doe John 128 Any Street AnyTown CA 12350
7 OG Dow Jane 129 Any Street AnyTown CA 12351
8 OGF Cool Joe 130 Any Street AnyTown CA 12352
9 SLV Cool Carol 131 Any Street AnyTown CA 12353
10 SMP Bravo Johnny 132 Any Street AnyTown CA 12354
11 3S Doe John 133 Any Street AnyTown CA 12355
12 OG Dow Jane 134 Any Street AnyTown CA 12356
13 OGF Cool Joe 135 Any Street AnyTown CA 12357
14 SLV Cool Carol 136 Any Street AnyTown CA 12358
15 SMP Bravo Johnny 137 Any Street AnyTown CA 12359
16 3S Doe John 138 Any Street AnyTown CA 12360
17 OG Dow Jane 139 Any Street AnyTown CA 12361
18 OGF Cool Joe 140 Any Street AnyTown CA 12362
19 SLV Cool Carol 141 Any Street AnyTown CA 12363
20 SMP Bravo Johnny 142 Any Street AnyTown CA 12364
輸出檔案應如下所示:
sequence art last first address city st zip
1 3S Doe John 123 Any Street AnyTown CA 12345
6 3S Doe John 128 Any Street AnyTown CA 12350
2 OG Dow Jane 124 Any Street AnyTown CA 12346
7 OG Dow Jane 129 Any Street AnyTown CA 12351
3 OGF Cool Joe 125 Any Street AnyTown CA 12347
8 OGF Cool Joe 130 Any Street AnyTown CA 12352
4 SLV Cool Carol 126 Any Street AnyTown CA 12348
9 SLV Cool Carol 131 Any Street AnyTown CA 12353
5 SMP Bravo Johnny 127 Any Street AnyTown CA 12349
10 SMP Bravo Johnny 132 Any Street AnyTown CA 12354
如您所見,該檔案已根據藝術列中的值過濾為 2 行。首先,匯入 csv 需要按藝術列排序,然后過濾到每個藝術值 2 行。
uj5u.com熱心網友回復:
通過查看您的輸出,您似乎希望按藝術列進行分組,然后從每個組中選擇前 2 個物件并輸出它們。為此,您可以結合使用.Group-Object
Select-Object
例如,給定您問題中的示例,使用以下代碼:
Import-Csv path\to\csv.csv | Group-Object art |
ForEach-Object { $_.Group | Select-Object -First 2 } |
Export-Csv path\to\export.csv -NoTypeInformation
輸出變為:
sequence art last first address city st zip
-------- --- ---- ----- ------- ---- -- ---
1 3S Doe John 123 Any Street AnyTown
6 3S Doe John 128 Any Street AnyTown
2 OG Dow Jane 124 Any Street AnyTown
7 OG Dow Jane 129 Any Street AnyTown
3 OGF Cool Joe 125 Any Street AnyTown
8 OGF Cool Joe 130 Any Street AnyTown
4 SLV Cool Carol 126 Any Street AnyTown
9 SLV Cool Carol 131 Any Street AnyTown
5 SMP Bravo Johnny 127 Any Street AnyTown
10 SMP Bravo Johnny 132 Any Street AnyTown
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524894.html