需要帮助来解决我收到的错误:
$ INPUTFILE:
JKAFDHLFEWQR,AFLJHDJB7269367,100.000000,13/01/2017
AKDSNIVVXCOI,ASJDFGYQTWEYTRS,100.000000,08/06/2017
POASDJFHDHFJ,PAOSDFPOIK12311,98.500000,08/06/2017
NIUCIADFUASD,KLJFGUWYE89,100.150000,15/02/2017
NACSDHFIUDYD,ASDFMJKH124,100.150000,02/12/2015
QWERYEUYREOQ,JDFSJ62736J,100.150000,04/12/2015
UQER76786ADS,MCVKJDUAFIO,100.150000,01/02/2016
NMCXHJKSDH67,ZNKSOQ,10.000000,13/04/2016
MAOJI2637868,BNIISA,10.000000,04/04/2016
CMASDHFUIQE1,AMSTER,10.000000,04/04/2016
JSFHIEUWRYJF,POQERUANSHJKSHD,100.000000,19/12/2016
23KJDKH7SD67,MKOAUSDUIFOIUQW,100.000000,12/12/2016
预期结果:(空格+ 0 +空格+数组[0] +带有前导零的数组[2]的内容)
0 JKAFDHLFEWQR 0000000000000100.000000
0 AKDSNIVVXCOI 0000000000000100.000000
0 POASDJFHDHFJ 0000000000000098.500000
0 NIUCIADFUASD 0000000000000100.150000
0 NACSDHFIUDYD 0000000000000100.150000
0 QWERYEUYREOQ 0000000000000100.150000
0 UQER76786ADS 0000000000000010.000000
0 NMCXHJKSDH67 0000000000000010.000000
0 MAOJI2637868 0000000000000010.000000
0 CMASDHFUIQE1 0000000000000010.000000
0 JSFHIEUWRYJF 0000000000000100.000000
0 23KJDKH7SD67 0000000000000100.000000
我的代码:
gc $InputFile | ForEach-Object {
$Value = $_ -split ','
$Val2 = $Value[2] | % PadLeft 23 '0'
$FileOut += $19spaces + $Value[0] + " " + $Val2 + "`n"
}
$FileOut.Split("`n") + $LastLine | Out-File $OutFile
$ LastLine在末尾添加一个尾随行:
0 0000000000000000.000000
代码完全按照我的需要提供了这些错误信息:
%:您无法在空值表达式上调用方法。 在 C:\ Desktop \ test.ps1:92 char:23 + $ Val2 = $ Value [2] | %PadLeft 23'0' + ~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(:) [ForEach-Object],PSArgumen tException + FullyQualifiedErrorId:InvokeMethodOnNull,Microsoft.PowerShell.Commands .ForEachObjectCommand
有关如何修复此错误的任何想法吗?谢谢大家!
答案 0 :(得分:2)
这个PowerShell单线程应该用
> import-csv .\input.txt -header H1,H2,H3,H4|%{" {0,-20}{1} {2}" -f 0,$_.H1,$_.H3.PadLeft(23,'0')}
0 JKAFDHLFEWQR 0000000000000100.000000
0 AKDSNIVVXCOI 0000000000000100.000000
0 POASDJFHDHFJ 0000000000000098.500000
0 NIUCIADFUASD 0000000000000100.150000
0 NACSDHFIUDYD 0000000000000100.150000
0 QWERYEUYREOQ 0000000000000100.150000
0 UQER76786ADS 0000000000000100.150000
0 NMCXHJKSDH67 0000000000000010.000000
0 MAOJI2637868 0000000000000010.000000
0 CMASDHFUIQE1 0000000000000010.000000
0 JSFHIEUWRYJF 0000000000000100.000000
0 23KJDKH7SD67 0000000000000100.000000
尾随空白行没有问题。
答案 1 :(得分:1)
我能找到重现此问题的唯一方法是在$InputFile
的内容中添加额外的行
要修复它,只需验证分割返回的内容不是$ null
$Value = $_ -split ','
if ($value -ne $null)
{
$Val2 = $Value[2] | % PadLeft 23 '0'
$FileOut += $19spaces + $Value[0] + " " + $Val2 + "`n"
}
答案 2 :(得分:0)
无需将文本传递给PadLeft。
$Val2 = $Value[2].PadLeft(23, '0')