简体   繁体   English

powershell 正则表达式匹配并更新脚本中的变量

[英]powershell regex match and update variables in script

I want to match and update variables in a script.我想匹配和更新脚本中的变量。 The script has variables like the examples below.该脚本具有类似于以下示例的变量。 I need a regex code that will match all 4 of those patterns in the script.我需要一个匹配脚本中所有 4 个模式的正则表达式代码。

variables to match & update in script [plus variations]:在脚本中匹配和更新的变量[加上变体]:

export LINUXSERVERFQDN='devserver.mtch.dam.gtmcs.com'
#LINUXSERVERFQDN variations = 'jsgsyuytrsg0122.jam.wert.gt.com' 'dftmhuayul0152.ejya.kam.tc.com' 'epwdfvwmiup0987.WDH.mpc.ad.pq.com' 'waupppm0jdg04.ypp.mpr.ws.qq.com'
export IPADDRESS='13.165.14.337'
export SITECODE='AGG'
export DATACOLLECTIONDBPWD='datacollection-micro-service-db-password'
#DATACOLLECTIONDBPWD variations = 'decknet-adaptor-micro-service-db-password' 'packing-micro-service-db-password' 'legacy-service-db-password' 'proficiency-service-db-password' 'traceability-service-db-password'

code:代码:

$variables = @(
        @{ name = "LINUXSERVERFQDN"; value = 'wbghdks1234.ppcmr.naq.gcc.com'; },
        @{ name = "IPADDRESS"; value = '10.353.76.111'; },
        @{ name = "SITECODE"; value = 'GGG'; },
        @{ name = "DATACOLLECTIONDBPWD"; value = 'hkjsiudsudiusd8sd8ui'; }
    )

    foreach ($variable in $variables)
    {

        (Get-Content -Path $filePath -Raw) -replace "$($variable.name)=(?-i)^[A-Z.]$", "$($variable.name)='$($variable.value)'" | Set-Content -Path $filePath
    }
  • I suggest replacing your regex (which doesn't quite work the way you want) with the following, which also simplifies the replacement operand:我建议用以下替换你的正则表达式(它并不完全按照你想要的方式工作),这也简化了替换操作数:

    • "(?m)(?<=^\s*export $($variable.name)=').*(?=')"

    • Note:笔记:

      • I'm assuming that replacements should only be performed on (not commented-out) export... lines.我假设只应在(未注释掉的) export...行上执行替换。

      • -creplace , the case- sensitive variant of the -replace operator is used below, obviating the need for (?-i) -creplace-replace运算符区分大小写的变体在下面使用,无需(?-i)

      • Look-around assertions ( (?<=...) and (?=...) ) are used to make the regex only capture the variable value to replace while asserting the presence of surrounding strings.环视断言( (?<=...)(?=...) )用于使正则表达式仅捕获要替换的变量,同时断言周围字符串的存在。

      • See this regex101.com page for an explanation of the regex and the ability to interact with it.请参阅此 regex101.com 页面,了解正则表达式及其交互能力的说明。

  • Additionally, for better performance, I suggest performing all replacements in memory first, before writing the result back to the input file.此外,为了获得更好的性能,我建议先执行memory 中的所有替换,然后再将结果写回输入文件。

$variables = @(
        @{ name = "LINUXSERVERFQDN"; value = 'wbghdks1234.ppcmr.naq.gcc.com'; },
        @{ name = "IPADDRESS"; value = '10.353.76.111'; },
        @{ name = "SITECODE"; value = 'GGG'; },
        @{ name = "DATACOLLECTIONDBPWD"; value = 'hkjsiudsudiusd8sd8ui'; }
    )

$fileContent = Get-Content -Raw -LiteralPath $filePath
foreach ($var in $variables) {
  $fileContent = 
    $fileContent -creplace "(?m)(?<=^\s*export $($var.name)=').*(?=')", $var.value
}

# Note: To be safe, "out.txt" is used as the output file.
#       Once you're sure the code works as intended, you can
#       replace it with $filePath to update the input file in place.
Set-Content -NoNewline -LiteralPath out.txt -Value $fileContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM