powershell脚本:查找和替换,以及在文件中插入行

nvbavucw  于 2023-03-02  发布在  Shell
关注(0)|答案(2)|浏览(132)
    • 背景**

我正在尝试编写一个小实用程序,它可以搜索文件以查找所有类似于以下内容的行的示例:

[SecuredEndpoint(widgets.UpdateWidget)]

让它看起来像这样:

//[SecuredEndpoint(widgets.UpdateWidget)]
 [AllowAnonymous]

到目前为止,我有以下脚本-还没有写回文件..只是先在内存中做所有的事情。

#Find and comment out all [SecuredEndpoint] attributes
(Get-Content -path .\TestController.cs -Raw) -replace '\[SecuredEndpoint','//\[SecuredEnpoint' | Foreach-Object {
        $_ # send the current line to output
        if ($_ -match "//[SecuredEndpoint") 
        {
            #Add Lines after the selected pattern 
            "[AllowAnonymous]"
        }
    }

它正确地注解掉了所有不同的SecuredEndpoint行,但之后并没有插入新行。

[Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }

我没有收到任何错误。

    • 问题**

我在插入新行的逻辑中遗漏了什么?一旦修复了这个问题,我假设我可以做一些类似这样的事情来将内容保存回文件:

(Get-Content -path .\TestController.cs -Raw) -replace '\[SecuredEndpoint','//[SecuredEnpoint' | Foreach-Object {
        $_ # send the current line to output
        if ($_ -match "//\[SecuredEndpoint") 
        {
            #Add Lines after the selected pattern 
            "[AllowAnonymous]"
        }
    } Set-Content .\TestController.cs

拜托,谢谢!

uplii1fm

uplii1fm1#

下面的正则表达式替换可以解决这个问题:

(Get-Content .\TestController.cs -Raw) -replace '\[SecuredEnpoint\(.+?\)]', "// `$0`n[AllowAnonymous]"

有关正则表达式的详细信息,请参见https://regex101.com/r/WZDaEx/2
如果要使用区分大小写的比较来匹配SecuredEnpoint,请改用-creplace
如果可能已经有注解[SecuredEnpoint(...)]行,并且您希望避免添加新的注解层,则可以使用以下命令:

(Get-Content .\TestController.cs -Raw) -replace '(?<!//\s*)\[SecuredEnpoint\(.+?\)]', "// `$0`n[AllowAnonymous]"

有关正则表达式的详细信息,请参见https://regex101.com/r/6gPgws/1

ogq8wdun

ogq8wdun2#

快速重构"@圣地亚哥Squarzon“有用答案。请尝试使用此方法。
好吧,如果有多个文件可能需要更新,以及与其他混合字符串内容。

'   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }
' | 
Out-File -FilePath 'D:\Temp\TestController.cs' -Force

'   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredRemoteEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredLocalEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }
' | 
Out-File -FilePath 'D:\Temp\TestController1.cs' -Force

Clear-Host
(Get-ChildItem -Path 'D:\Temp' -Filter '*TestController*.cs').
ForEach(
    {
        "********* Updating $(($PSItem).Name) content *********"

        Get-Content -Path $PSItem.Fullname 

        (Get-Content $PSItem.Fullname -Raw) -replace '\[Secured.*\]', "`$0`n`t[AllowAnonymous]" | 
        Set-Content -Path $PSItem.Fullname

        Get-Content -Path $PSItem.Fullname 
    }
)

# Results
<#
********* Updating TestController.cs content *********
   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }

   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredEnpoint(mywidget.GetAll)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredEnpoint(mywidget.Create)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }

********* Updating TestController1.cs content *********
   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredRemoteEnpoint(mywidget.GetAll)]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredLocalEnpoint(mywidget.Create)]
    public List<Widgets> GetWidgets()
    {
    }

   
    [Route("widgets/")]
    [HttpGet]
    //[SecuredRemoteEnpoint(mywidget.GetAll)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }

    [Route("widgets/)]
    [HttpPost]
    //[SecuredLocalEnpoint(mywidget.Create)]
    [AllowAnonymous]
    public List<Widgets> GetWidgets()
    {
    }
#>

相关问题