管道

2016-09-29 86 views
1

替換文本,我有以下腳本我使用刪除混帳合併分支:管道

function Remove-MergedBranches 
{ 
    git branch -a --merged | 
    ForEach-Object { $_.Trim() } | 
    Where-Object {$_ -NotMatch "^\*"} | 
    Where-Object {-not ($_ -Like "*master")} | 
    Where-Object {-not ($_ -Like "*develop")} | 
    Where-Object {-not ($_ -Like "*dev")} | 
    % {$_.replace("/remotes","")} | 
    ForEach-Object { git branch -d $_ } 
} 

replace不工作。

一個分支可能是remotes/origin/tg-training-section

回答

3

您wan't更換/remotesremotes/origin/tg-training-section不匹配(它不以斜線開始),所以你可能想省略斜線:

# .... 
% {$_.replace("remotes","")} 

另一種方式是通過使用-replace用正則表達式替換/remotesremotes

# .... 
% {$_ -replace '\/?remotes'} 

我還會考慮甚至使用\b\/?remotes以確保您不會替換字符串中的remotes