powershell - replace string in array for C# model -


i wrote powershell script inserts newtonsoft json annotation above each line.

starting file:

public class rootobject {     public string clientid { get; set; }     public string bankid { get; set; }     public string applicationid { get; set; }     //(...)     public string appname { get; set; }     public generaldata loandatarequest { get; set; } }  public class roles {     public string role1 { get; set; }     public string otherparty1 { get; set; } } 

result file isn't correct:

public class rootobject     {  [jsonproperty(propertyname = "")] public string clientid { get; set; }  [jsonproperty(propertyname = "")] public string bankid { get; set; }  [jsonproperty(propertyname = "")]         public string applicationid { get; set; } ... } //other properties exluded due simplicity of code 

the script:

$filename = "e:\startingfile.txt" $fileoriginal = get-content $filename $lines = (get-content e:\startingfile.txt) #trim lines $newcontent = foreach ($line in $lines) {     $line.trim() }  ($i = 0; $i -lt $fileoriginal.length; $i++) {     if ($fileoriginal[$i] -like "*public*" -and $fileoriginal[$i] -notlike "*class*")  {         # insert line before line , not insert if line contains '{','}'         $fileoriginal[$i] -replace 'public', '`npublic'         $fileoriginal[$i] -replace '{', '`n{'         $newfilecontent += "`n[jsonproperty(propertyname = """ + $fileoriginal[$i].split()[2] + """)]"     }      $newfilecontent += $fileoriginal[$i] }  $newfilecontent | out-file "e:\resultfile.txt"  

result file want be:

public class rootobject     {    [jsonproperty(propertyname = "clientid ")]   public string clientid { get; set; }    [jsonproperty(propertyname = "bankid ")]   public string bankid { get; set; }    [jsonproperty(propertyname = "applicationid ")]           public string applicationid { get; set; }   ... } //other properties exluded due simplicity of code 

questions:

  1. why won't add $fileoriginal[$i].split()[2] json propertyname?

    answer (editied): realized line contains multiple blanks can value $fileoriginal[$i].split()[10].

  2. how replace array $lines[3] element , capitalize first letter? (public string clientid -> public string clientid)

  3. how format txt output same formatted startingfile?

don't bother inserting elements array or building output string. insert new lines output stream go. also, if want indention preserved: don't trim leading whitespace.

get-content 'e:\startingfile.txt' | foreach-object {     if ($_ -like '*public*' -and $_ -notlike '*class*') {         if ($_ -match '^(\s*)(public\s+\w+\s+)(\w)(\w*)(.*)') {             '{0}[jsonproperty(propertyname = "{1}{2}")]' -f $matches[1,3,4]             $_ = '{0}{1}{2}{3}{4}' -f $matches[1], $matches[2], $matches[3].toupper(), $matches[4], $matches[5]         }     }     $_ } | set-content 'e:\resultfile.txt' 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -