Home > Mobile >  Replace method is not working on my string variables
Replace method is not working on my string variables

Time:01-21

I wrote a script to extract the URL and Revision Number from svn info command of a svn repository and save the result in a .txt file.

The $revision and $url are both strings, so the replace method should work on them but it doesn't. Is there possibly something wrong in my code causing this?

$TheFilePath = "C:\Users\MyPC\REPOSITORY\NewProject\OUTPUT.txt"

echo "#- Automatic Package Update `n----------"| Out-File -FilePath $TheFilePath
 
$url = svn info C:\Users\MyPC\REPOSITORY\NewProject\trunk | Select-String -Pattern 'URL' -CaseSensitive -SimpleMatch | select-object -First 1 
$url | Add-Content -path $TheFilePath
$revision = svn info C:\Users\MyPC\REPOSITORY\NewProject\trunk | Select-String -Pattern 'Revision' -CaseSensitive -SimpleMatch | $revision.Replace('Revision','srcrev')
$revision | Add-Content -path $TheFilePath 

here is the output of svn info (Irrelevant outputs have been omitted) :

Path: .
Working Copy Root Path: C:\Users\MyPC\REPOSITORY\NewProject\trunk
URL: https://svn.mycompany.de/svn/NewProject/trunk
Relative URL: ^/trunk
Repository Root: https://svn.mycompany.de/svn/NewProject
Revision: 5884

And here is what I get inside the .txt file , running the code :

#- Automatic Package Update 
----------
URL: https://svn.mycompany.de/svn/NewProject/trunk
Revision: 5884
----------

CodePudding user response:

Looking at the example output of svn info here and the example you just provided, you should be able to get the info you need easier with ConvertFrom-StringData then with Select-String.

In PowerShell < 7.x you can use ConvertFrom-StringData on the output of svn info after changing the colon (:) delimiter into an equal sign (=) to get a Hashtable with all properties and values. Then, using calculated properties you can extract the items you're interested in and save as CSV file for instance like this:

$svnInfo = svn info 'C:\Users\MyPC\REPOSITORY\NewProject\trunk'
$result  = $svnInfo -replace '(?<!:.*):', '=' | ConvertFrom-StringData | 
           Select-Object @{Name = 'URL'; Expression = {$_['URL']}}, 
                         @{Name = 'srcrev'; Expression = {$_['Revision']}}

# output on screen
$result | Format-Table -AutoSize

# output to CSV file
$result | Export-Csv -Path 'C:\Users\MyPC\REPOSITORIES\NewProject\OUTPUT.csv' -NoTypeInformation

Regex details on the -replace to replace only the first occurrence of the colon:

(?<!        Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   :        Match the character “:” literally
   .        Match any single character
      *     Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)          
:           Match the character “:” literally

If you're using PowerShell 7 or higher, tghings get easier because then you have an extra -Delimiter parameter:

$svnInfo = svn info 'C:\Users\MyPC\REPOSITORY\NewProject\trunk'
$result  = $svnInfo -replace '(?<!:.*):', '=' | ConvertFrom-StringData -Delimiter ':' | 
           Select-Object @{Name = 'URL'; Expression = {$_['URL']}}, 
                         @{Name = 'srcrev'; Expression = {$_['Revision']}}
# output on screen
$result | Format-Table -AutoSize

# output to CSV file
$result | Export-Csv -Path 'C:\Users\MyPC\REPOSITORIES\NewProject\OUTPUT.csv' -NoTypeInformation

CodePudding user response:

$revision doesn't exist until after the svn command is done.

Use the ForEach-Object cmdlet and refer to the current match as $_ to modify the output object inline - the matched line in the output from Select-String is stored in a property called Line:

$revision = svn info C:\Users\MyPC\REPOSITORY\NewProject\trunk |Select-String -Pattern 'Revision' -CaseSensitive -SimpleMatch |ForEach-Object { $_.Line.Replace('Revision', 'srcrev') }
  • Related