这使我的输出文件加倍!我怎样才能解决这个问题?

时间:2018-05-22 15:16:44

标签: powershell powershell-v3.0

它一直在查看您的源文件,然后将这些更改添加到您的输出中,因为源文件没有更改,它会不断将这些更改添加到输出

 $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?

我不希望我的文件改变大小..所有上述更改只需要在一个输出文件上完成

2 个答案:

答案 0 :(得分:1)

这是否会产生预期的结果?

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

答案 1 :(得分:0)

将下面的脚本放在PowerShell .ps1文件中。

$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
    }

修改

--Append上应该出现Out-File标记。

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

修改

如果您不熟悉PowerShell,运行此脚本的最简单方法是通过以下步骤。

  1. 将PowerShell脚本复制/粘贴到名为script.ps1的文件中。
  2. 将以下内容复制/粘贴到名为runscript.cmd的文件中。

    @echo off
    SET psfile="%~dp0script.ps1"
    PowerShell.exe -ExecutionPolicy ByPass -File %psfile%
    pause
    
  3. 从命令提示符处执行runscript.cmd