簡體   English   中英

電源殼| 在特定模式后從 .txt 中刪除行

[英]Powershell | Delete lines from .txt after specific pattern

InputObject   : created_at : Tue Sep 24 02:31:26 +0000 2020
SideIndicator : =>

InputObject   : text       : CH#66551 by @Test: Testing this script to see if it works
SideIndicator : =>

InputObject   : created_at : Tue Sep 24 00:27:01 +0000 2020
SideIndicator : =>

InputObject   : text       : CH#34330 by Test: Fixed every thing that is able to breathe
SideIndicator : =>

InputObject   : created_at : Tue Sep 22 02:31:26 +0000 2020

InputObject   : text       : CH#66551 by @User2: Fixing stuff

InputObject   : created_at : Tue Sep 22 00:27:01 +0000 2020

InputObject   : text       : CH#34330 by User1: Seeing if it works

InputObject   : created_at : Mon Sep 21 15:54:20 +0000 2020

InputObject   : text       : CH#34294 by User1: Trying to find a workaround

InputObject   : created_at : Mon Sep 21 15:29:13 +0000 2020

InputObject   : text       : CH#34291 by User3: Doing something else

InputObject   : created_at : Mon Sep 21 15:03:15 +0000 2020

InputObject   : text       : CH#34286 by User3: Running around

InputObject   : 

InputObject   : 

我想刪除此 .txt 文件中最后一個“SideIndicator : =>”之后的所有內容,非常感謝我的幫助,因為我是 PowerShell 新手。

您可以簡單地執行以下操作:

(Get-Content file.txt -Raw | Select-String -pattern '(?s).*SideIndicator : =>').Matches.Value |
    Set-Content newfile.txt

解釋:

使用-Raw允許將文件作為單個字符串讀入。 這使單行模式(?s)能夠有效地工作。 單行模式允許. 匹配換行符,這使得跨多行匹配批量字符變得容易。

由於Select-String返回MatchInfo對象,因此訪問Matches屬性的Value屬性僅返回匹配的文本。

抱歉,我看錯了您的要求並重寫了代碼

也許不是最優雅的方法(我是新手......)但我測試了它並且它根據您的要求工作:

$path="C:\test\test.txt" #contains the data you ve shown in your question
$newPath="C:\test\test1.txt" #servs as a testfile to see if the data is correct

$end=(Get-Content -Path $path -Raw).LastIndexOf("SideIndicator : =>")
$length=("SideIndicator : =>").Length


(Get-Content -Path $path -Raw).Substring(0,$end+$length) | Add-Content -Path $newPath

這是獲得我認為你想要的東西的一種方法。 由於您沒有准確指定您想要什么,我抓住了與SideIndicator關聯的兩行並SideIndicator構建了一個[PSCustomObject] 如果您想要更少的信息,請根據需要修改該部分。 [咧嘴笑]

它能做什么 ...

  • 在文本文件中偽造閱讀
    當准備好使用真實數據執行此操作時,將整個#region/#endregion塊替換為對Get-Content的調用。
  • 設定目標以決定保留什么
  • 遍歷行
  • 目標測試
  • 如果找到,則構建一個包含所需行的[PSCustomObject]
  • 如果未找到,則不執行任何操作
  • PSCO發送到$Result集合
  • 在屏幕上顯示

編碼 ...

#region >>> fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
InputObject   : created_at : Tue Sep 24 02:31:26 +0000 2020
SideIndicator : =>

InputObject   : text       : CH#66551 by @Test: Testing this script to see if it works
SideIndicator : =>

InputObject   : created_at : Tue Sep 24 00:27:01 +0000 2020
SideIndicator : =>

InputObject   : text       : CH#34330 by Test: Fixed every thing that is able to breathe
SideIndicator : =>

InputObject   : created_at : Tue Sep 22 02:31:26 +0000 2020

InputObject   : text       : CH#66551 by @User2: Fixing stuff

InputObject   : created_at : Tue Sep 22 00:27:01 +0000 2020

InputObject   : text       : CH#34330 by User1: Seeing if it works

InputObject   : created_at : Mon Sep 21 15:54:20 +0000 2020

InputObject   : text       : CH#34294 by User1: Trying to find a workaround

InputObject   : created_at : Mon Sep 21 15:29:13 +0000 2020

InputObject   : text       : CH#34291 by User3: Doing something else

InputObject   : created_at : Mon Sep 21 15:03:15 +0000 2020

InputObject   : text       : CH#34286 by User3: Running around

InputObject   : 

InputObject   : 
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a text file

$Target = 'SideIndicator'

$Result = foreach ($Index in 0..$InStuff.GetUpperBound(0))
    {
    if ($InStuff[$Index] -match $Target)
        {
        [PSCustomObject]@{
            IO_Line = $InStuff[$Index -1]
            SI_Line = $InStuff[$Index]
            }
        }
    } # end >>> foreach ($Index in 0..$InStuff.GetUpperBound(0))

$Result

輸出 ...

IO_Line                                                                                  SI_Line           
-------                                                                                  -------           
InputObject   : created_at : Tue Sep 24 02:31:26 +0000 2020                              SideIndicator : =>
InputObject   : text       : CH#66551 by @Test: Testing this script to see if it works   SideIndicator : =>
InputObject   : created_at : Tue Sep 24 00:27:01 +0000 2020                              SideIndicator : =>
InputObject   : text       : CH#34330 by Test: Fixed every thing that is able to breathe SideIndicator : =>

另一種方法是使用LastIndexOf()

$txt = Get-Content -Path 'TheFullPathOfTheTxtFile' -Raw
$lastIndex = $txt.LastIndexOf("SideIndicator")
if ($lastIndex -ge 0) {
    # create a new text file here
    # $txt.Substring(0, ($lastIndex + 18)) | Set-Content -Path 'TheFullPathOfThe_NEW_TxtFile'
    # for demo, just output on console screen
    $txt.Substring(0, ($lastIndex + 18)) # 18 = length of "SideIndicator : =>"
}

或使用Select-String

$txt = Get-Content -Path 'TheFullPathOfTheTxtFile' -Raw
$txt.Substring(0,($txt | Select-String -Pattern '(?m)^SideIndicator' -AllMatches).Matches[-1].Index + 18)

我要感謝你們所有人抽出時間來幫助我。 昨天我實際上找到了一個(另一個)解決方案(僅比較對象左側或右側),我測試了它並且效果很好。 也期待嘗試您的建議;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM