String Powershell。匹配后替换下一个字符串

String Powershell。匹配后替换下一个字符串,string,powershell,replace,match,String,Powershell,Replace,Match,我的listing.lst包含数千行。我需要替换一行,这是动态变化和位置从结束和开始的文件也在变化。 但是有一个常量字符串“a”,“a”(有几个,我需要最后一个),如果我从下面走两行,我可以找到目标行,这必须更改 $log = 'c:\rep\listing.lst' $match = '"a","a"' $string = Select-String $match $log | ForEach-Object {$_.LineNumber + 1} | Select-Object -Last 1

我的listing.lst包含数千行。我需要替换一行,这是动态变化和位置从结束和开始的文件也在变化。 但是有一个常量字符串“a”,“a”(有几个,我需要最后一个),如果我从下面走两行,我可以找到目标行,这必须更改

$log = 'c:\rep\listing.lst'
$match = '"a","a"'
$string = Select-String $match $log | ForEach-Object {$_.LineNumber + 1} | Select-Object -Last 1
(Get-Content $log)[$string]

我能够找到这个字符串,但不明白如何将这个目标字符串更改为例如“on”

这是我不久前构建的一个函数,用于查找和替换文本文件中的字符串。至少,传入文件路径、要查找的字符串和替换该字符串的字符串,这样您就成功了:

Find-InTextFile -FilePath C:\MyFile.txt -Find '"a","a"' -Replace 'on'

function Find-InTextFile
{
    <#
    .SYNOPSIS
        Performs a find (or replace) on a string in a text file or files.
    .EXAMPLE
        PS> Find-InTextFile -FilePath 'C:\MyFile.txt' -Find 'water' -Replace 'wine'

        Replaces all instances of the string 'water' into the string 'wine' in
        'C:\MyFile.txt'.
    .EXAMPLE
        PS> Find-InTextFile -FilePath 'C:\MyFile.txt' -Find 'water'

        Finds all instances of the string 'water' in the file 'C:\MyFile.txt'.
    .PARAMETER FilePath
        The file path of the text file you'd like to perform a find/replace on.
    .PARAMETER Find
        The string you'd like to replace.
    .PARAMETER Replace
        The string you'd like to replace your 'Find' string with.
    .PARAMETER UseRegex
        Use this switch parameter if you're finding strings using regex else the Find string will
        be escaped from regex characters
    .PARAMETER NewFilePath
        If a new file with the replaced the string needs to be created instead of replacing
        the contents of the existing file use this param to create a new file.
    .PARAMETER Force
        If the NewFilePath param is used using this param will overwrite any file that
        exists in NewFilePath.
    #>
    [CmdletBinding(DefaultParameterSetName = 'NewFile')]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateScript({ Test-Path -Path $_ -PathType 'Leaf' })]
        [string[]]$FilePath,

        [Parameter(Mandatory = $true)]
        [string]$Find,

        [Parameter()]
        [string]$Replace,

        [Parameter()]
        [switch]$UseRegex,

        [Parameter(ParameterSetName = 'NewFile')]
        [ValidateScript({ Test-Path -Path ($_ | Split-Path -Parent) -PathType 'Container' })]
        [string]$NewFilePath,

        [Parameter(ParameterSetName = 'NewFile')]
        [switch]$Force
    )
    begin
    {
        $SystemTempFolderPath = Get-SystemTempFolderPath
        if (!$UseRegex.IsPresent)
        {
            $Find = [regex]::Escape($Find)
        }
    }
    process
    {
        try
        {
            Write-Log -Message "$($MyInvocation.MyCommand) - BEGIN"
            foreach ($File in $FilePath)
            {
                if ($Replace)
                {
                    if ($NewFilePath)
                    {
                        if ((Test-Path -Path $NewFilePath -PathType 'Leaf') -and $Force.IsPresent)
                        {
                            Remove-Item -Path $NewFilePath -Force
                            (Get-Content $File) -replace $Find, $Replace | Add-Content -Path $NewFilePath -Force
                        }
                        elseif ((Test-Path -Path $NewFilePath -PathType 'Leaf') -and !$Force.IsPresent)
                        {
                            Write-Warning "The file at '$NewFilePath' already exists and the -Force param was not used"
                        }
                        else
                        {
                            (Get-Content $File) -replace $Find, $Replace | Add-Content -Path $NewFilePath -Force
                        }
                    }
                    else
                    {
                        (Get-Content $File) -replace $Find, $Replace | Add-Content -Path "$File.tmp" -Force
                        Remove-Item -Path $File
                        Rename-Item -Path "$File.tmp" -NewName $File
                    }
                }
                else
                {
                    Select-String -Path $File -Pattern $Find
                }
            }
            Write-Log -Message "$($MyInvocation.MyCommand) - END"
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            Write-Log -Message "$($MyInvocation.MyCommand) - END"
            $false
        }
    }
}
Find InTextFile-FilePath C:\MyFile.txt-Find''a','a'-替换'on'
函数Find InTextFile
{
查找InTextFile-FilePath“C:\MyFile.txt”-查找“水”-替换“酒”
将字符串“water”的所有实例替换为中的字符串“wine”
“C:\MyFile.txt”。
.举例
PS>Find InTextFile-FilePath'C:\MyFile.txt'-Find'water'
在文件“C:\MyFile.txt”中查找字符串“water”的所有实例。
.参数文件路径
要对其执行查找/替换的文本文件的文件路径。
.参数查找
要替换的字符串。
.参数替换
要将“查找”字符串替换为的字符串。
.参数UseRegex
如果要使用regex查找字符串,请使用此开关参数,否则查找字符串将
从正则表达式字符中转义
.参数NewFilePath
如果新文件已替换,则需要创建字符串,而不是替换
现有文件的内容使用此参数创建新文件。
.参数力
如果使用NewFilePath参数,则使用此参数将覆盖
存在于NewFilePath中。
#>
[CmdletBinding(DefaultParameterSetName='NewFile')]
param(
[参数(必需=$true)]
[ValidateScript({Test Path-Path$\ ux-PathType'Leaf'})]
[string[]]$FilePath,
[参数(必需=$true)]
[字符串]$Find,
[参数()]
[字符串]$Replace,
[参数()]
[开关]$UseRegex,
[参数(ParameterSetName='NewFile')]
[ValidateScript({Test Path-Path($| Split Path-Parent)-PathType'Container}]
[字符串]$NewFilePath,
[参数(ParameterSetName='NewFile')]
[开关]$Force
)
开始
{
$SystemTempFolderPath=获取SystemTempFolderPath
如果(!$UseRegex.IsPresent)
{
$Find=[regex]::转义($Find)
}
}
过程
{
尝试
{
写入日志-消息“$($MyInvocation.MyCommand)-BEGIN”
foreach($FilePath中的文件)
{
如果($更换)
{
if($NewFilePath)
{
if((测试路径-路径$NewFilePath-路径类型'Leaf')-和$Force.IsPresent)
{
删除项-Path$NewFilePath-Force
(获取内容$File)-replace$Find,$replace | Add Content-Path$NewFilePath-Force
}
elseif((测试路径-路径$NewFilePath-路径类型'Leaf')-和!$Force.IsPresent)
{
写入警告“位于“$NewFilePath”的文件已存在,并且未使用-Force参数”
}
其他的
{
(获取内容$File)-replace$Find,$replace | Add Content-Path$NewFilePath-Force
}
}
其他的
{
(获取内容$File)-替换$Find,$replace |添加内容-路径“$File.tmp”-强制
删除项-路径$File
重命名项-路径“$File.tmp”-新名称$File
}
}
其他的
{
选择字符串-路径$File-模式$Find
}
}
写入日志-消息“$($MyInvocation.MyCommand)-END”
}
抓住
{
写入日志-消息“错误:$($).Exception.Message)-行号:$($).InvocationInfo.ScriptLineNumber)”-日志级别“3”
写入日志-消息“$($MyInvocation.MyCommand)-END”
$false
}
}
}