title: PowerShell Search and Replace yaml
body: <p>I am looking for some solution to this Problem Statement please</p>
  <p>Let say we have two files, one is in yaml format and have specific placeholder, for ex - (member_name). Please note, since the file format is in yaml it will have certain amount of blank spaces before the placeholder.</p>
  <p>Then we have a separate file that has the actual name of the member in this format – member_name: abc xyz. This file does not have additional spaces.</p>
  <p>What I need is, to replace the content of yaml file, placeholder, without disturbing the file structure, with the corresponding values from the second file.</p>
  <p>Like the yaml file after execution should have member_name: abc xyz, along with the spaces in the original file.</p>
  <p>This has to go in loop since I have multiple placeholders in the file A and corresponding values in the second file.</p>
  <p>Let me know if I am not clear please.</p>
  <p>This is what I have tried out...</p>
  <pre><code>$realmember = GC &quot;\Path\to\file\realMember.txt&quot;
  $yamlfile = GC &quot;\Path\to\file\yamlFile&quot;
  
  foreach($line in $yamlfile){
  
      $script:found += @($line | select-string -Pattern '(*)$' | %{$_ -replace '(\s*','*'} | %{$_ -replace ')','*'})
  }
  
  for($i = 0; $i -lt ($realMember.Count); $i++){
  
      if($realMember[$i] -like $found[$i].Trim()){
  
          $secrets = $realMember[$i]
  
          $file = $file -replace &quot;[^\s*].*[^\s*]&lt;*&gt;$&quot;,&quot;$realMember[$i]&quot; #| Where-Object {$_ -match &quot;$realMember[$i]&quot;}
  
      }
  
  }
  
  $file
  </code></pre>
  <p>but this prints result in following format, all values in each of the field. I need to find out if there is a way to include variables with regex while replacing OR any other better ways to achieve this</p>
  <pre><code>MemberData:
    member_id: 01
    member_name_1: abc def member_name_2: ghi jkl member_name_3: mno pqr member_name_4: stu vwx[x]
    member_id: 02
    member_name_1: abc def member_name_2: ghi jkl member_name_3: mno pqr member_name_4: stu vwx[x]
    new_members:
      member_id: 003
      member_name_1: abc def member_name_2: ghi jkl member_name_3: mno pqr member_name_4: stu vwx[x]
      member_id: 004
      member_name_1: abc def member_name_2: ghi jkl member_name_3: mno pqr member_name_4: stu vwx[x]</pre></code>
  <p>===========================</p>
  <p><strong>realMember.txt content</strong></p>
  <pre><code>member_name_1: abc def
  member_name_2: ghi jkl
  member_name_3: mno pqr
  member_name_4: stu vwx</pre></code>
  <p>===========================</p>
  <p><strong>yamlFile content</strong></p>
  <pre><code>MemberData:
    member_id: 01
    (member_name_1)
    member_id: 02
    (member_name_2)
    new_members:
     member_id: 003
     (member_name_3)
     member_id: 004
     (member_name_4)</pre></code>