Home > Software engineering >  powershell regex match and update variables in script
powershell regex match and update variables in script

Time:08-19

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.

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
    }

CodePudding user response:

  • 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.

      • -creplace, the case-sensitive variant of the -replace operator is used below, obviating the need for (?-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.

  • Additionally, for better performance, I suggest performing all replacements in memory first, before writing the result back to the input file.

$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
  • Related