在文本文件中添加一行新文本

时间:2018-10-02 07:31:05

标签: powershell

我有此功能可以查看文档内部。基本上,我正在寻找一些缺失的数据并将其添加。 例如,完整的数据如下所示:

SubTotal :           30,473.00
%IVA     :            3,960.00
%SER     :            3,047.00
TOTALES  :           37,483.00

但是,如果缺少任何这些行,我必须将其添加为零值。到目前为止,这是寻找它的功能。似乎可以正常工作,但是我不知道该怎么做,如何使用PowerShell将丢失的数据添加到该特定行中

功能如下:

    function leeyedita() 
    {
        $content = Get-Content $root\UNB\FINAL_TEXTO\FACT_FINAL.txt
        for($i = 0; $i -lt $content.Count; $i++) {
           $line = $content[$i]
           $nextLine = $content[$i+1]

           if ($line.StartsWith("SubTotal")-and ($nextline.StartsWith("%SER"))) {
               Write-Host "NO ENCONTRE IVA"
               Write-Host $content[$i]
               # Replace the line with the Replacement Line Text.
           } 

           elseif ($line.StartsWith("%IVA")-and ($nextline.StartsWith("TOTALES"))) {
                Write-Host "NO ENCONTRE SERVICIO"
           }    

           else {
              #Write-Host "NO ES"
           }
       }
    }

Thanks a lot for the help!

UPDATE: 

Sometimes the file can have 2 totals :

    TOTALES  :         14,383.00
    Descuento Aplicado:          1,598.00
    TOTALES :     14,383.00


I want to delete the second one, so I created an if condition 

    elseif ($line.StartsWith("Descuento Aplicado")-and ($nextline.StartsWith("TOTALES"))) {
            Replace the line with the Replacement Line Text.
            Write-Host "ENCONTRE DESCUENTO" -fore white 
            $newcontent += ""

       }

That would create a blank line. But I want to delete the line. Is there a way?
Thanks!

2018年10月8日更新:

我正面临一个新问题。问题是,当文件不提供服务(%SER)且您应用了折扣时,脚本将写入正确的0 ammount,但不会删除第一个总计。

我每次获取时都需要删除第一个总计(仅当折扣(Descuento Aplicado)在文件中具有值时才会发生。我不知道为什么它不执行满足该条件的else if函数...

elseif ($line.StartsWith("TOTALES")-and (!$previousLine.StartsWith("DescuentoAplicado")-and ($booldesc-eq 1))) 



SubTotal:           8,122.00
%IVA    :             959.00
%SER    :             0.00
TOTALES :           9,073.00
DescuentoAplicado:             918.00
TOTALES :      9,073.00
(COLONES)

这是更新的代码:

   function leeyedita() 
{
    write-host "FUNCION LEE Y EDITA TXT" -fore white 
    $content = Get-Content $root\UNB\FINAL_TEXTO\FACT_FINAL.txt
    $newcontent = @()
    $boolserv=0
    $booliva=0
    $boolpropina=0
    $booldesc=0



    for($i = 0; $i -lt $content.Count; $i++){

       $line = $content[$i]

     if ($line.StartsWith("DescuentoAplicado"))

       {
       $booldesc=1
       }

     elseif ($line.StartsWith("%IVA"))

       {
       $booliva=1
       }

     elseif ($line.StartsWith("%SER"))

       {
       $boolserv=1
       }

     elseif ($line.StartsWith("PROPINA"))

       {
       $boolpropina=1
       }


}
Write-Host "SERV $boolserv IVA $booliva PROP $boolpropina DESC $booldesc"

    for($i = 0; $i -lt $content.Count; $i++){

       $line = $content[$i]
       $previousLine = $content[$i-1]
       $nextLine = $content[$i+1]


       if ($line.StartsWith("SubTotal")-and ($booliva-eq 0)) {
           # Replace the line with the Replacement Line Text.
            Write-Host "NO ENCONTRE IVA" -fore white            
            $newcontent += $line 
            $newcontent += '{0, 0}'-f"%IVA : 0.00"
            $booliva=1

       }


       elseif ($line.StartsWith("TOTALES")-and ($boolserv-eq 0)) {
           # Replace the line with the Replacement Line Text.
            Write-Host "NO ENCONTRE SERVICIO" -fore white 
            $newcontent += '{0, 0}'-f"%SER : 0.00"
            $newcontent += $line
            $boolserv=1 


       }

       elseif ($line.StartsWith("TOTALES")-and($booldesc-eq 0)) {
           # Replace the line with the Replacement Line Text.
            Write-Host "NO ENCONTRE DESCUENTO" -fore white 
            $newcontent += '{0, 0}'-f"DescuentoAplicado : 0.00"
            $newcontent += $line
            $booldesc=1 
       }

       elseif ($line.StartsWith("TOTALES")-and (!$previousLine.StartsWith("DescuentoAplicado")-and ($booldesc-eq 1))) {
           # Replace the line with the Replacement Line Text.
            Write-Host "ENCONTRE DESCUENTO" -fore white


       }


       else {
            #Write-Host "LINEA NORMAL"
            $newcontent += $line
            }



            #Write-Host "ESCRIBIENDO ARCHIVO"
            $newcontent | out-file $root\UNB\FINAL_TEXTO\FACT_FINAL.txt   

       } 


    }

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是在遍历$newcontent的内容之前初始化一个空数组($content),用所需的内容填充$newcontent,然后将其输出到文本文件:

function leeyedita() 
{
    $content = Get-Content $root\UNB\FINAL_TEXTO\FACT_FINAL.txt

    # Initialize empty array
    $newcontent = @()

    for($i = 0; $i -lt $content.Count; $i++) {
       $line = $content[$i]
       $nextLine = $content[$i+1]

       if ($line.StartsWith("SubTotal")-and ($nextline.StartsWith("%SER"))) {
           Write-Host "NO ENCONTRE IVA"
           Write-Host $content[$i]

           # Add the current line and then the missing IVA line 
           $newcontent += $line
           $newcontent += "%IVA : 0.00"
       } 

       elseif ($line.StartsWith("%IVA")-and ($nextline.StartsWith("TOTALES"))) {
            Write-Host "NO ENCONTRE SERVICIO"

            # Add the current line and then the missing SER line 
            $newcontent += $line 
            $newcontent += "%SER : 0.00"
       }    

       else {
          #Write-Host "NO ES"

          # Add current line
          $newcontent += $line
       }
   }

   # Output new content to file
   $newcontent | out-file $root\UNB\FINAL_TEXTO\FACT_FINAL.txt   
}

如果您希望以与其他文本条目相同的方式对齐新文本,请查看how to format strings in powershell