简体   繁体   English

Powershell匹配正则表达式和替换

[英]Powershell match Regex and Replace

I have a large file that I am searching through to locate and replace invalid dates. 我正在搜索一个大文件,以查找和替换无效的日期。 I'm using a REGEX expression to locate the dates and then determining if they are valid or not. 我正在使用REGEX表达式定位日期,然后确定日期是否有效。 If the script finds an invalid date it needs to replace the date with the current date. 如果脚本发现无效的日期,则需要用当前日期替换该日期。 For audit purposes I need to record the invalid string and the line number on which the error was found. 出于审计目的,我需要记录无效的字符串和发现错误的行号。 So far (with some prior help to SO) I have been able to locate the invalid dates, but I have not been able to successfully change them. 到目前为止(在SO的一些先前帮助下),我已经能够找到无效的日期,但是我无法成功地更改它们。

This is the code I'm using to locate the invalid dates. 这是我用来查找无效日期的代码。 How can I locate and change the date in a single pass? 如何单次查找和更改日期?

$matchInfos = @(Select-String -Pattern $regex -AllMatches -Path $file)
foreach ($minfo in $matchInfos)
{
    #"LineNumber $($minfo.LineNumber)"
    foreach ($match in @($minfo.Matches | Foreach {$_.Groups[0].value}))
    {
        if (([Boolean]($match -as [DateTime]) -eq $false ) -or ([DateTime]::parseexact($match,"MM-dd-yyyy",$null).Year -lt "1800")) {
            Write-host "Invalid date on line $($minfo.LineNumber) - $match"
            #Add-Content -Path $LOGFILE -Value "Invalid date on line $($minfo.LineNumber) - $match"

            # Replace the invalid date with a corrected one
            Write-Host "Replacing $match with $(Get-Date -Format "MM-dd-yyyy")"
            #Add-Content -Path $LOGFILE -Value "Replacing $match with $(Get-Date -Format "MM-dd-yyyy")"

        }
    }
 }

You have to write out a temporary file with the changes and replace the file with the temporary. 您必须写出具有更改的临时文件,然后用临时文件替换该文件。 Here's one I wrote that will do that part for you: 这是我写的,将为您完成这一工作:

Windows IT Pro: Replacing Strings in Files Using PowerShell Windows IT Pro:使用PowerShell替换文件中的字符串

Example of use: 使用示例:

replace-filestring -pattern 'find' -replacement 'replace' -path myfile.txt -overwrite

With this command, the script will read myfile.txt, replace 'find' with 'replace', write the output to a temporary file, and then replace myfile.txt with the temporary file. 使用此命令,脚本将读取myfile.txt,将“ find”替换为“ replace”,将输出写入临时文件,然后将临时文件替换为myfile.txt。 (Without the -Overwrite parameter, the script will only output the contents of myfile.txt with the changes.) (没有-Overwrite参数,脚本将仅输出带有更改的myfile.txt的内容。)

Bill 法案

$lines = get-content $file
$len = $lines.count
$bad = @{}
for($i=0;$i-lt$len;$i++){
    if($lines[$i] -match ""){
        $bad_date = $lines[$i].substring(10) #Get the bad date
        $good_date = Get-Date -Format G
        $bad["$i"] += @($line[$i])
        $lines[$i] = $lines[$i].Replace($bad_date,$good_date)
    }
}
$lines > $NewFile
$bad > $bad_date_file

Here is some pseudo code of how I would combat this problem. 这是一些有关如何解决此问题的伪代码。 Not sure how big your file is. 不确定文件的大小。 Reading and writing could be slow. 读写可能很慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM