Chaitanya

Chaitanya

  • NA
  • 58
  • 57.1k

Regex or best way to simplify the code to extract and append

Sep 30 2016 4:17 AM
I do have my text as follows in my config file
  1. [ {root, [ {child, 123} ]} ]. or []   
when the text is [] I am doing as follows
 
  1.  $fileName = Get-ChildItem -Path "$filePath" -Recurse -Filter "fileName.txt"  
  2.  $resultMatch = Select-String -Path $fileName.FullName -Pattern "root"  
  3.   
  4. if([string]::IsNullOrWhiteSpace($resultMatch))  
  5. {  
  6.     $content=''  
  7.     $content = "[  
  8.     {root,  
  9.    [  
  10.     {child, 123} // this value may change  
  11.    ]}  
  12.  ]."  
  13.   
  14.     Clear-Content $newfileName  
  15.     Add-Content  $newfileName $content  
  16. }  
 If root was found i need to check for child child if not exists I need to append else add. 

I am doing as follows $resultMatch = Select-String -Path $fileName.FullName -Pattern "{child,"

I am thinking of a better way to do it, so any one having any idea.

 
The entire file will have as follows
  1. [  
  2.  {root,  
  3.       {child, 0}, if exists replace the entire line else add  
  4.       {child, 1}  
  5.   ]}  
  6. ].  
 

Answers (1)