簡體   English   中英

基於CSV字典(Powershell)的每個文件替換字符串?

[英]For-each file replace string based on a CSV Dictionary (Powershell)?

努力配置轉換腳本,其中根據 CSV 文件中的內容在文件中完成替換。 就像字典或查找一樣,而不是直接存儲在代碼中。

即File1.txt

Jim
Tim
Jan
Greg
Mark

CSV 文件

DEV,UAT,PROD
John,Jimothy,Timothy
Jimothy,Frank,Karen
Jim,Max,Lisa

因此,如果轉換 DEV > UAT file1.txt 會將 Jim 替換為 Max:

Max
Tim
Jan
Greg
Mark

以下是我目前所在的地方 convertReferences.ps1

#Declare Function Library
. $PSScriptRoot\functionLibrary.ps1
#Get Variables
$global:Dictionary = Import-Csv -Path $PSScriptRoot\IDDictionary.csv
#Input Location
$InputLocation = Read-Host 'Enter Input Location' ['DEV/UAT']
    If(!(test-path $PSScriptRoot\$InputLocation))
        {
        New-Item -ItemType Directory -Force -Path $PSScriptRoot\$InputLocation
        }
#Get Output Location
$OutLocation = Read-Host 'Enter an Output Location' ['UAT/PROD']
    
        If(!(test-path $PSScriptRoot\$OutLocation))
        {
         New-Item -ItemType Directory -Force -Path $PSScriptRoot\$OutLocation
        }

#Call Function Convert-DEV
if ($InputLocation -eq 'DEV'){
$Files | convert-DEV -InputLocation $InputLocation -OutLocation $OutLocation}

else {Write-host "NO VALID INPUT DECLARED - PLEASE RUN AGAIN" -ForegroundColor RED
    <# Action when all if and elseif conditions are false #>
}

函數本身如下:

function convert-DEV {
[cmdletbinding()]
param(
    #Input Path
    [Parameter(Mandatory)]
    [ValidateSet('DEV')]
    [string]
    $InputLocation,

    #Files
    [Parameter(Mandatory, ValueFromPipeline)]
    [string]
    $Files,

    #Output Path
    [parameter()]
    [ValidateSet('UAT')]
    [string]
    $OutLocation
)
process{
        Write-host "Replacing Variables in: " $Files -ForegroundColor Blue
        $Staging = $Files
        (Get-Content $Files | foreach {$_ -replace $Global:Dictionary.DEV , $Global:Dictionary.UAT}) |
        Set-Content $Files
        (Get-Content $Staging | foreach {Copy-Item -Path $Staging -Destination $PSScriptRoot\$OutLocation})
        Write-host "UPDATED File has been copied to: " -ForegroundColor Red $PSScriptRoot\$OutLocation `n `n
} 
}

關於如何達到我想要的輸出的任何想法?

您可以使用此功能開始,這是假設 CSV 和文件放置在同一位置:

$csv = Import-Csv .\ReferenceTable.csv

function Replace {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [string] $File,

        [Parameter(Mandatory)]
        [ValidateSet('DEV', 'UAT', 'PROD')]
        [string] $InputLocation,

        [Parameter(Mandatory)]
        [ValidateSet('DEV', 'UAT', 'PROD')]
        [string] $OutLocation,

        [Parameter(Mandatory)]
        [object[]] $ReferenceTable
    )

    begin {
        $map = @{}
        foreach($i in $ReferenceTable) {
            $map[$i.$InputLocation] = $i.$OutLocation
        }
    }
    process {
        foreach($line in (Get-Content $File).Trim()) {
            if($map.ContainsKey($line)) {
                $map[$line]
                continue
            }
            $line
        }
    }
}

Get-ChildItem .\File1.txt | Replace -InputLocation DEV -OutLocation UAT -ReferenceTable $csv

經過一些進一步的工作和上面聖地亞哥的幫助后,我得到了以下內容,可以按我的意願工作:

function Start-Replace {
[cmdletbinding()]
param(
    [Parameter(Mandatory, ValueFromPipeline)]
    [string] $File,

    [Parameter(Mandatory)]
    [ValidateSet('DEV','UAT','PROD')]
    [string] $InputLocation,

    [Parameter(Mandatory)]
    [ValidateSet('DEV', 'UAT', 'PROD')]
    [string] $OutLocation,

    [Parameter(Mandatory)]
    [object[]] $ReferenceTable
)

begin {
    #Create Map Hashtable/array
    $map = @{}
    #For each row in Reference list select the value in the input column and add to map array. After match and add output column value to array
    foreach($row in $ReferenceTable) 
    {
        $map[$row.$InputLocation] = $row.$OutLocation
    }

}

#Replace
 process {
    $outname = split-path $file -leaf
     $input = Get-Content $file
    Foreach ($key in $map.Keys) {
        $input = $input.Replace($key, $map.$key)
     }
     Set-Content -Path $PSScriptRoot\$OutLocation\$outlocation-$outname -value $input
     write-host $outlocation'-'$outname

 }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM