作为输出的新文件-如何在PowerShell中创建它

作为输出的新文件-如何在PowerShell中创建它,powershell,formatting,output,Powershell,Formatting,Output,所以我有这个输入文件(没有文件扩展名): 我想从那里得到三个值,然后用特定的格式打印在一个新文件上 我正在尝试使用substring选项,但是我不能像预期的那样在输出文件上看到结果,所以这段代码可能有什么问题 我希望看到的结果如下: 1 2 3 4 5 1234567890123456789012345678901234567890123456789012345 CODE123

所以我有这个输入文件(没有文件扩展名):

我想从那里得到三个值,然后用特定的格式打印在一个新文件上

我正在尝试使用substring选项,但是我不能像预期的那样在输出文件上看到结果,所以这段代码可能有什么问题

我希望看到的结果如下:

         1         2         3         4         5
1234567890123456789012345678901234567890123456789012345
                 CODE123                          17.98
                 CODE12                            2.37
                 CODE                              2.70
                 CODE12                            2.73
                 CODE123                          13.39
但是使用下面的代码,我得到的结果没有列。一行字就把事情搞砸了

$InputFile = D:\Test\inputfile.txt
$outFile = D:\Test\outfile.txt
$Pattern = "010TESTS"
$spaces1 = "                     ";
$spaces2 = "                    ";
$dot = "."
$rec = 0

(gc $InputFile) | ? {$_.trim() -ne "" } | Select-String -pattern $Pattern -AllMatches | Set-Content $outFile

$file = gc $outFile

$Test = $file.Substring(69, 8)  # This should give me the value 010TESTS

foreach ($line in $file) {

    if ($Test -eq "010TESTS") {

        $Value1 = $file.Substring(57, 8)
        $Value2 = $file.Substring(137, 6).TrimStart('0') # To remove leading zeroes
        $Value3 = $file.Substring(143, 2)

        if (! ($Value1 -eq "" -and $Value2 -eq "00")) {

            # Here I need the values on each line
            $FinalFile += ($spaces1 + $Value1 + $spaces2 + $Value2 + $dot + $Value3).Split("`n")   
        }
    }
}

$FinalFile | Set-Content $outFile
  • 子字符串偏移量与示例数据不匹配
  • 要检查偏移,应(至少)临时插入一个
    标尺
  • 要用空格填充定义长度的字符串,只需执行
    $spaces=“”*21
  • 但正如您被告知最好使用
    -f
    格式字符串作为输出,请参阅脚本
  • 不要摆弄两个值并插入一个点,只需获得整个范围并除以100(忽略前导零)

  • $\包含Forach对象当前处理的行
  • [string]强制转换(转换)为字符串,这是
    .substring
  • 花括号中的第一个数字表示变量跟随
    -f
  • 逗号后花括号中的第二个数字表示变量将占用的位置(负数表示左对齐,正右对齐)
  • 冒号后面是格式说明符(请参阅),N2表示带2位小数的浮点数
由于我的语言环境,此示例输出有一个十进制逗号。(手动添加标尺)


$file
(输入数组)和
$file
(输出字符串)应该是不同的变量吗?PowerShell不区分大小写。如果您需要输出文件的固定格式,我强烈建议您也浏览,您只需在循环外设置一次
$Test
。因为它的值在循环中从不改变,所以
if()
每次都采用相同的分支。哼哼,我会看看你提到的$Test,谢谢。在创建输出文件时,你不能将
D:\Test
作为一个输入文件,同时也有一个目录
D:\Test
。LotPings非常感谢你提供了宝贵的提示,我不太了解-f格式,但必须学习,那么在这段代码中它到底意味着什么呢?“{0,25}{1,30:N2}”我也不明白这部分,它做什么$Line=[string]$\u为您的问题添加了一些注释。太好了!这一切现在都有意义了,再次感谢你的解释,帮了很多忙。
$InputFile = D:\Test\inputfile.txt
$outFile = D:\Test\outfile.txt
$Pattern = "010TESTS"
$spaces1 = "                     ";
$spaces2 = "                    ";
$dot = "."
$rec = 0

(gc $InputFile) | ? {$_.trim() -ne "" } | Select-String -pattern $Pattern -AllMatches | Set-Content $outFile

$file = gc $outFile

$Test = $file.Substring(69, 8)  # This should give me the value 010TESTS

foreach ($line in $file) {

    if ($Test -eq "010TESTS") {

        $Value1 = $file.Substring(57, 8)
        $Value2 = $file.Substring(137, 6).TrimStart('0') # To remove leading zeroes
        $Value3 = $file.Substring(143, 2)

        if (! ($Value1 -eq "" -and $Value2 -eq "00")) {

            # Here I need the values on each line
            $FinalFile += ($spaces1 + $Value1 + $spaces2 + $Value2 + $dot + $Value3).Split("`n")   
        }
    }
}

$FinalFile | Set-Content $outFile
$InputFile = 'D:\Test\inputfile.txt'
$outFile =   'D:\Test\outfile.txt'
$Pattern = "010TESTS"

Get-Content $InputFile | Select-String -Pattern $Pattern | ForEach-Object {
    $Line = [string]$_
    $Line.Substring(70, 8)  #this should give me the value 010TESTS
    $Value1 = $Line.Substring(57, 8)
    $Value2 = [double]$Line.Substring(137, 8) / 100

    if ( $Value1 -and $Value2 ) {
      "{0,25}{1,30:N2}" -f $Value1,$Value2 | Out-File $OutFile -Append
    }
}
         1         2         3         4         5     
1234567890123456789012345678901234567890123456789012345
                 CODE123                          14,83
                 CODE123                           3,36
                 CODE123                           1,20
                 CODE123                          41,24
                 CODE123                           1,02