powershell - 如何在 powershell 中打开 csv 文件并删除包含空白数据的行、更改列顺序并写出结果
问题描述
我想打开一个 csv 文件,检查行的内容是否有缺失数据,如果数据缺失则排除该行,然后以与我阅读的顺序不同的顺序写出列。
例如,这是输入 CSV 文件:
"givenName","sn","userPrincipalName","telephoneNumber"
,,"Administrator@engeo.com",
"Citrix","Scheduler Service","citrixscheduler@engeo.com",
,,,
,,,
"dbagent",,"dbagent@engeo.com",
"Cory","Montini","cmontini@engeo.com","925-395-2566"
这是我希望的最终结果:
"userPrincipalName","sn","givenName","telephoneNumber"
"cmontini@engeo.com","Montini","Cory","925-395-2566"
这是我拥有的代码:
$Path = 'c:\temp\ps\Test.csv'
Import-Csv -Path $Path | ForEach-Object {
If ($_.givenName -eq '' ) {
# how do I remove the line
}
If ($_.ns -eq '' ) {
# how do I remove the line
}
If ($_.userPrincipalName -eq '' ) {
# how do I remove the line
}
If ($_.telephoneNumber -eq '' ) {
# how do I remove the line
}
$_
} | Format-Table -AutoSize
(Import-CSV -Path $Path) | Select-Object -Property userPrincipalName, givenName, sn, telephoneNumber | Export-CSV -Path $Path
我当前的输出
正在重新排序列...但不删除数据,我不知道为什么 #TYPE Selected.System.Management.Automation.PSCustomObject 在第一行
#TYPE Selected.System.Management.Automation.PSCustomObject
"userPrincipalName","givenName","sn","telephoneNumber"
"Administrator@engeo.com","","",""
"citrixscheduler@engeo.com","Citrix","Scheduler Service",""
"","","",""
"","","",""
"dbagent@engeo.com","dbagent","",""
"cmontini@engeo.com","Cory","Montini","925-395-2566"
更新 1
我把代码改成了这个
$Path = 'c:\temp\ps\Test.csv'
Import-Csv -Path $Path | Where-Object {
$_.userPrincipalName -and $_.givenName -and $_.sn -and $_.telephoneNumber
} | ForEach-Object {
$_
} | Format-Table -AutoSize
(Import-CSV -Path $Path) | Select-Object -Property userPrincipalName, givenName, sn, telephoneNumber | Export-CSV -Path $Path
现在我在 Powershell 中得到了正确的输出:
givenName sn userPrincipalName telephoneNumber
--------- -- ----------------- ---------------
Cory Montini cmontini@engeo.com 925-395-2566
但我的输出文件仍然被顶起
更新 2
这是最新的代码:
$Path = 'c:\temp\ps\Test.csv'
$Temp = 'c:\temp\ps\_temp.csv'
Import-Csv -Path $Path | Where-Object {
$_.userPrincipalName -and $_.givenName -and $_.sn -and $_.telephoneNumber
} | ForEach-Object {
$_
} | Export-Csv -Path $Temp -NoTypeInformation
Remove-Item -Path $Path
Rename-Item -Path $Temp -NewName $Path
(Import-CSV -Path $Path) | Select-Object -Property userPrincipalName, givenName, sn, telephoneNumber | Export-CSV -Path $Path
我越来越近了......这是输出,但第一行是从哪里来的?
#TYPE Selected.System.Management.Automation.PSCustomObject
"userPrincipalName","givenName","sn","telephoneNumber"
"cmontini@engeo.com","Cory","Montini","925-395-2566"
解决方案
有很多方法,但快速简单的一种方法是:
$csv = Import-Csv [...] # add your csv path
$csv | ? {$_.givenName -ne '' -and $_.sn -ne '' `
-and $_.telephoneNumber -ne '' `
-and $_.userPrincipalName -ne ''} |
Select userPrincipalName,sn,givenName,telephoneNumber
| Export-CSV -Path $Path -NoTypeInformation
输出:
userPrincipalName sn givenName telephoneNumber
----------------- -- --------- ---------------
cmontini@engeo.com Montini Cory 925-395-2566
推荐阅读
- ios - iOS - superView 的相对顶部空间
- json - 使用哪种结构来解析 JSON 中的数据?
- javascript - 如何在 Angular 中调整 mat-header-cell 宽度?
- node.js - 我如何在 Laravel (5.*) 中使用普通的 SQL 查询?或者我应该学习 ORM 吗?
- elasticsearch - 7.x 版本中的 Elasticsearch 日期解析错误
- javascript - 如何输入文件,然后对其进行过滤并将结果输出到文本区域?
- excel - 如何在excel中获取用户输入的序列号
- c++ - c++ 中的 Potency-function 丢弃了令人困惑的结果
- r - 因子水平的抖动箱线图加上组合水平的箱线图
- java - 是否可以通过 Apache POI 将 XSSFPivotTable 连接到外部 DataSheet(位于外部工作簿)?