[英]Powershell delete text in string that does not match conditions
我有一個文本文件,我需要逐行檢查並刪除引號中的任何文本並且與以下值之一不匹配:
我的初始文件看起來像這樣:
IPADDRESS "192.168.10.1" "192.168.11.1" "192.168.12.1" "192.168.13.1" "10.0.0.1"
IPADDRESS "192.168.10.1" "192.168.11.1" "127.0.0.1" "192.168.12.1" "192.168.13.1"
我希望我的輸出類似於以下內容:
IPADDRESS "192.168.10.1" "192.168.11.1" "192.168.12.1" "192.168.13.1"
IPADDRESS "192.168.10.1" "192.168.11.1" "192.168.12.1" "192.168.13.1"
我已經嘗試過使用 -replace 選項、正則表達式、使用修剪選項、使用過濾器、使用 -match 選項,取得了不同程度的成功,但我一直在碰壁……
您可以拆分該行,使用正則表達式刪除不需要的部分並重新組裝該行:
Get-Content ip.txt | Where-Object {$_} | Foreach-Object {
$line=$_.Split()
$line[0] + ' ' + ($line -match '"192\.168\.1[0-3]\.1"') -join ' '
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.