通过逐行删除并在日期比较为true时停止使用PowerShell清除大型日志文件 [英] Purge with PowerShell a big log file by deleting line by line and stopping it when my date comparison is true

查看:203
本文介绍了通过逐行删除并在日期比较为true时停止使用PowerShell清除大型日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天来,我正在研究不同的脚本,以将行删除到不同的日志文件中.但是我仍然有一个我无法处理的文件,因为该结构比我清除的其他文件更复杂. 为了给您一个主意,我在此处放置了该日志文件的一些示例行.

During these days i'm working on different scripts to delete lines into different log files. But i have still one file that i can't handle because the structure is bit more complex than the others files i have purged. To give you an idea i put here some example lines of that log file.

[ 30-10-2017 16:38:07.62 | INFO    | Some text
[ 30-10-2017 16:38:11.07 | INFO    | Some text
[1];Erreur XXXX non-gérée : Some text.
Merci de communiquer some text :
 - Some text again
 - Identifiant : XXXXXXXX-1789-XXXXX-b41b-XXXX. >> Some text
[ 02-11-2017 16:38:11.10 | INFO    | Some text
[ 02-11-2017 16:38:11.10 | INFO    | Some text
[2];88852228343 / some text
[ 03-11-2017 16:38:11.10 | INFO    | Some text
[ 03-11-2017 16:38:11.10 | INFO    | Some text
Other text here
And other one
[ 04-11-2017 16:38:11.10 | INFO    | Some text
[ 04-11-2017 16:38:11.10 | INFO    | Some text

我尝试了一些东西,但是我认为这不是正确的方法->

I have a tried something but i think it was not the right way to doing it -> How to catch terminating error and still continue my PowerShell script

该文件为4Mo,现在执行此操作的代码不起作用,因为我做错了方法.我首先执行一个子字符串来提取日期,然后进行比较,但是当我碰到不是以日期开头的行时,我会报错并且脚本停止

The file is 4Mo and the code to do it right now is not working because i do it the wrong way. I do first a substring to extract date then compare but when i hit a line which is not starting by a date i have an error and the script stop

try
    {
        (Get-Content $Fichier) |
            Where-Object {$_} |
            Where-Object { ([datetime]::ParseExact(([string]$_).Substring(2,19), $Format, $Culture) -ge (Get-Date).AddDays(-$Jours)) } |
            Set-Content $Fichier
        LogMessage -Message "Fichier $Fichier purgé des toutes les lignes datant de plus de $Jours jours"
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        LogMessage -Message "$FailedItem - $ErrorMessage"
    }

我认为,如果我发现大于或等于(今天-20天)的日期,那么开始阅读文件并开始删除每一行然后停止删除文件的其余部分会更好. 但是现在我找不到在PowerShell中实现此目标的正确方法.

I think it will be better if i start reading file and start deleting each line and then stop to delete the rest of the file when i find a date which is greater or equal than (today - 20 days). But right now i can't find a proper way to achieve it in PowerShell.

推荐答案

您建议的解析每一行的方法,直到您发现一个20天或更短的日期可能会起作用.我会做这样的事情:

Your suggested approach of parsing each line until you find a date that's 20 days or less old would probably work. I'd do something like this:

# Placeholder for the dates we're about to parse
$DT = Get-Date
# Define the threshold once
$Limit = (Get-Date).AddDays(-$Jours)
# We'll use this flag to keep track of whether we've reached the limit or not
$Keep = $false

(Get-Content $Fichier |ForEach-Object {
    if($Keep){
        $_
    }
    elseif([datetime]::TryParseExact($_.Substring(2,19), $Format, $Culture, $null, [ref]$DT) -and $DT -ge $Limit){
        $Keep = $true
        $_
    }
}) |Set-Content $Fichier

这篇关于通过逐行删除并在日期比较为true时停止使用PowerShell清除大型日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆