需要从使用powershell转换为文本的CSV列中删除所有前导零



下面的代码添加了两列和行,但是当.CSV文件转换为txt时,有一个员工id列将它们列为:

00001  
00002  
00003  
etc.. Until we get to 10000. 

我需要删除txt文件中的前导零,以便它们读作…

1  
2  
3  
4  
etc. 

代码

Import-Csv ECMEmployeeExtract.csv |
Select-Object *,@{Name='Internal';Expression={'True'}} |
Select-Object *,@{Name='Location Type';Expression={'Person'}} |
Export-csv ECMEmployeeExtractUpdate.txt  -NoTypeInformation -Encoding Unicode -Delimiter '

csv

EMPLID  NAME    LAST NAME   FIRST NAME  MIDDLE NAME NAME SUFFIX REHIRE DT   TERMINATION DT
00001   Phillips, Henry Phillips    Henry           07/23/1962  07/21/1977
00002   Smith, Oscar    Smith   Oscar           03/01/1955  03/10/1988
00006   Kuhfahl, Herman Kuhfahl Herman          11/16/1969  11/17/1990

您可以使用以下计算属性来获得您期望的结果:

@{n='EMPLID';e={[int]$_.EMPLID}}

@{n='EMPLID';e={$_.EMPLID.TrimStart('0')}}
  • 示例:
@'
EMPLID  NAME    LAST NAME   FIRST NAME  MIDDLE NAME  NAME SUFFIX  REHIRE DT   TERMINATION DT
00001   Phillips, Henry  Phillips    Henry           07/23/1962  07/21/1977
00002   Smith, Oscar     Smith    Oscar           03/01/1955  03/10/1988
00006   Kuhfahl, Herman  Kuhfahl  Herman          11/16/1969  11/17/1990
Kuhfahl, Herman  Kuhfahl  Herman          11/16/1969  11/17/1990
'@ -replace '  +',',' | ConvertFrom-Csv |
Select-Object @{n='EMPLID';e={[int]$_.EMPLID}},* -ExcludeProperty EMPLID
  • 结果:
EMPLID NAME     LAST NAME FIRST NAME MIDDLE NAME NAME SUFFIX REHIRE DT  TERMINATION DT
------ ----     --------- ---------- ----------- ----------- ---------  --------------
1 Phillips Henry     Phillips   Henry       07/23/1962  07/21/1977               
2 Smith    Oscar     Smith      Oscar       03/01/1955  03/10/1988               
6 Kuhfahl  Herman    Kuhfahl    Herman      11/16/1969  11/17/1990               
0 Kuhfahl  Herman    Kuhfahl    Herman      11/16/1969  11/17/1990           

请记住,如果EMPLID没有值,则如果您使用[int]$_.EMPLID,则会将默认值设置为0(请参阅上面示例中的最后一行(,如果您使用的是$_.EMPLID.TrimStart('0'),则会设置为空值


实际代码应该如下所示:

Import-Csv ECMEmployeeExtract.csv |
Select-Object @{n='EMPLID';e={[int]$_.EMPLID}},*,
@{Name='Internal';Expression={'True'}},
@{Name='Location Type';Expression={'Person'}} -ExcludeProperty EMPLID |
Export-Csv ECMEmployeeExtractUpdate.csv -NoTypeInformation

这应该会给你这个结果:

EMPLID NAME     LAST NAME FIRST NAME MIDDLE NAME NAME SUFFIX REHIRE DT  TERMINATION DT Internal Location Type
------ ----     --------- ---------- ----------- ----------- ---------  -------------- -------- -------------
1 Phillips Henry     Phillips   Henry       07/23/1962  07/21/1977                True     Person       
2 Smith    Oscar     Smith      Oscar       03/01/1955  03/10/1988                True     Person       
6 Kuhfahl  Herman    Kuhfahl    Herman      11/16/1969  11/17/1990                True     Person       
0 Kuhfahl  Herman    Kuhfahl    Herman      11/16/1969  11/17/1990                True     Person   

相关内容

  • 没有找到相关文章

最新更新