For-each文件替换字符串基于CSV字典(Powershell)?



正在配置一个转换脚本,其中根据CSV文件中的内容在文件中完成替换。像字典或查找,而不是直接存储在代码中。

。中

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

下面是我现在的位置convertreference .ps1

#Declare Function Library
. $PSScriptRootfunctionLibrary.ps1
#Get Variables
$global:Dictionary = Import-Csv -Path $PSScriptRootIDDictionary.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和File放置在相同的位置:

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

}

相关内容

  • 没有找到相关文章

最新更新