This keeps doubling my output file!! how can i fix this?

≯℡__Kan透↙ 提交于 2019-12-13 09:08:00

问题


It keeps looking at your source file, then adding those changes to your output, since the source file doesn't change, it keeps adding those to the output

 $data = @(
        @{
            pattern = "LACTION 'SQL\(''logility_prod_scp_logility'',"
            replacement = "LACTION 'SQL(''LOGILITY_PROD_SCP_LOGILITY'',"
            inputFile = "C:\files\DW_FEI_input1.ctg"
            outputFile = "C:\files\DW_FEI_output1.ctg"
        },
        @{
            pattern = "LACTION 'SQL\(''dwfei'',"
            replacement = "LACTION 'SQL(''DW_FEI'',"
            inputFile = "C:\files\DW_FEI_output1.ctg"
            outputFile = "C:\files\DW_FEI_output2.ctg"
        },    
        @{
            pattern = "LACTION 'SQL\(''DWFEI'',"
            replacement = "LACTION 'SQL(''DW_FEI'',"
            inputFile = "C:\files\DW_FEI_output2.ctg"
            outputFile = "C:\files\DW_FEI_output3.ctg"
        }

    )

    $data | 
        ForEach-Object  { 
            (Get-Content $_.inputFile) -replace $_.pattern, $_.replacement | Out-File $_.outputFile
        }
    I used above code..trying many other way's as well..how can i make sure my file sizes are not doubled?

I don't want my file to change size..all the above changes need's to be done on one output file only


回答1:


Does this produce the expected results?

$pattern = "LACTION 'SQL\(''DWFEI'',"
$replacement = "LACTION 'SQL(''DW_FEI'',"
(Get-Content "inputfile.txt") -replace $pattern,$replacement |
  Out-File "newfile.txt"



回答2:


Put the script below in a PowerShell .ps1 file.

$data = @(
    @{
        pattern = "LACTION 'SQL\(''logility_prod_scp_logility'',"
        replacement = "LACTION 'SQL(''LOGILITY_PROD_SCP_LOGILITY'',"
        inputFile = "C:\files\DW_FEI_input.ctg"
        outPutFile = "C:\files\DW_FEI_output.ctg"
    },
    @{
        pattern = "LACTION 'SQL\(''dwfei'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\DW_FEI_input.ctg"
        outPutFile = "C:\files\DW_FEI_output.ctg"
    },    
    @{
        pattern = "LACTION 'SQL\(''DWFEI'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\DW_FEI_input.ctg"
        outputFile = "C:\files\DW_FEI_output.ctg"
    },
    @{
        pattern = "LACTION 'SQL\(''DWFEI'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"

    },
    @{
        pattern = "LACTION 'SQL\(''dwfei'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    },    
    @{
        pattern = "LACTION 'SQL\(''user_shared'',"
        replacement = "LACTION 'SQL(''USER_SHARED'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"

    },
    @{
        pattern = "LACTION 'SQL\(''DW_FEI_PROD'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    },
    @{
        pattern = "LACTION'SQL\(''SALES_MART_PROD'',"
        replacement = "LACTION 'SQL(''SALES_MART'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    },    
    @{
        pattern = "LACTION 'SQL\(''sales_mart'',"
        replacement = "LACTION 'SQL(''SALES_MART'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    }
)

$data | 
    ForEach-Object  { 
        (Get-Content $_.inputFile) -replace $_.pattern, $_.replacement | Out-File $_.outputFile
    }

Edit

The --Append flag should be present on the Out-File.

$data | 
    ForEach-Object  { 
        (Get-Content $_.inputFile) -replace $_.pattern, $_.replacement | Out-File $_.outputFile --append
    }

Edit

If you are not familiar with PowerShell, the easiest way to run this script is via the steps below.

  1. Copy/paste the PowerShell script in a file with name script.ps1.
  2. Copy/paste the content below in a file with name runscript.cmd.

    @echo off
    SET psfile="%~dp0script.ps1"
    PowerShell.exe -ExecutionPolicy ByPass -File %psfile%
    pause
    
  3. Execute runscript.cmd from the command prompt.



来源:https://stackoverflow.com/questions/50471182/this-keeps-doubling-my-output-file-how-can-i-fix-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!