1

I have text file which contains some keys of an hash table in the form of ${key}. I need to parse the text file and replace ${key} with it's value. Below is the code i am using

    #$config is the hashtable
   $document = Get-Content sample.txt
    foreach ($conf in $config.Keys) {
        [string]$match = '${' + $conf + '}'
        $document = $document.Replace($match, $($config.$conf))
        }
    }

I have tried below alternatives for $match string

[string]$match = "`${" + $conf + "}"

[string]$match='${'
$match+=$conf
$match+='}'

I even tried hard coding the string as $match='${BACKUP_FOLDER}' but in all the cases it does nothing. I have also tried -match to see if something matches but it always returns false. I have confirmed that the text file contains several patterns of ${key} format and if i copy the output of $match and search in the file i can find the patterns. Could you please point out what's wrong. I am using Powershell V5.0

Sample input

$config=@{"BACKUP_DIR"="_Backup";"STAGING_DIR"="_Staging"}

Contents of sample.txt

Overwrites the contents of new installation with old installation 
    Always use / as path seperator-->
    <!-- <rename path="" newName=""/> -->
    <!-- 
    ${BACKUP_DIR}
    rename-> rename file/directory in new installation

Output: ${BACKUP_DIR} should be replaced with _Backup

1
  • Please edit the question and add sample input and desired output data. Commented Jul 25, 2017 at 11:45

1 Answer 1

1

Looks like there're a couple of typos in the hashtable. Following is a complete solution you can save as script and run, or copy to console.

$document = @'
Overwrites the contents of new installation with old installation 
    Always use / as path seperator-->
    <!-- <rename path="" newName=""/> -->
    <!-- 
    ${BACKUP_FOLDER}
    rename-> rename file/directory in new installation
'@

$config=@{"BACKUP_FOLDER"="_Backup";"STAGING_DIR"="_Staging"}

Write-Output "`n`nINPUT`n$document"

foreach ($conf in $config.Keys) {
        [string]$match = '${' + $conf + '}'
        $document = $document.Replace($match, $config.$conf)
}

Write-Output "`n`nOUTPUT`n$document"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @gms0ulman. Typo was just in the question. Your script works when i run it as standalone script but when i incorporate in my main script it's not working. Looks like the issue is something else. Some more head scratching to be done :(
@PramodAhanya To confirm; have you noticed the different 2nd argument in .Replace, $config.$conf?
sorry another typo in question. i am using $config.$conf in the script.
@PramodAhanya BACKUP_DIR in hashtable and BACKUP_FOLDER in config also a typo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.