powershell Replace方法对字符串变量不起作用

raogr8fs  于 2023-02-08  发布在  Shell
关注(0)|答案(2)|浏览(129)

我编写了一个脚本,从svn仓库的svn info命令中提取URL和Revision Number,并将结果保存在一个.txt文件中.
$revision$url都是字符串,所以replace方法应该对它们起作用,但它没有。是否可能是我的代码中有什么错误导致了这一点?

$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

下面是svn info的输出(无关的输出被省略了):

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

下面是我在.txt文件中运行代码时得到的结果:

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

8e2ybdfx1#

看看svn info here的输出示例和您刚才提供的示例,您应该可以使用ConvertFrom-StringData比使用Select-String更容易地获得所需的信息.
在PowerShell〈7.x中,您可以在将冒号(:)分隔符更改为等号(=)后,对svn info的输出使用ConvertFrom-StringData,以获得包含所有属性和值的Hashtable。然后,使用计算属性,您可以提取感兴趣的项目并保存为CSV文件,例如:

$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

-replace的正则表达式详细信息,仅替换第一个出现的冒号:

(?<!        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

如果您使用的是PowerShell 7或更高版本,则操作会变得更简单,因为您有一个额外的-Delimiter参数:

$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
vmdwslir

vmdwslir2#

$revisionsvn命令完成 * 之后 * 才存在。
使用ForEach-Object cmdlet并将当前匹配引用为$_,以内联方式修改输出对象-Select-String输出中的匹配行存储在名为Line的属性中:

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

相关问题