![](/img/trans.png)
[英]I need to get a a specific value from Json file using powershell
[英]i want to get particular information from a text file using powershell
我有一個使用get-eventlog cmdlet提取的日志文件,這是其輸出示例:
index Time EntryType source InstanceID Message
000 xxxx error system 1111 host ip: 55.55.55.55
class: tropical
state: open
name: contractor_0001
000 xxxx error system 1111 host ip: 55.55.55.55
class: tropical
state: open
name: contractor_0002
000 xxxx error system 1111 host ip: 55.55.55.55
class: tropical
state: open
name: contractor_0003
000 xxxx error system 1111 host ip: 55.55.55.55
class: tropical
state: open
name: contractor_0004
基本上,我需要文本列表中每個人的名字,或者僅使用powershell進行輸出,例如:
contractor_0001
contractor_0002
contractor_0003
contractor_0004
我只需要名稱,不需要其他信息。
直接從事件日志中提取此信息會更簡單:
Get-EventLog "System" -EntryType "Error" -InstanceId 1111 | % {
$_.Message -replace '[\s\S]*name:\s+(\S+)[\s\S]*','$1'
}
如果限於顯示的文本文件,則可以嘗試使用Select-String
:
Select-String 'name:\s+(\S+)' input.txt | % { $_.Matches.Groups[1].Value }
正如其他人所說,直接從日志文件中提取所需的信息可能更好。
但是,要回答您提出的問題,可以這樣做:-
(gc ("C:\yourpath\yourfile.txt")) -match 'name:\s' | foreach {$_.Split(":")[1].Replace(' ','')}
您可以根據需要將其通過管道傳輸到文件外以寫入文件。
我的測試輸出的數據:
contractor_0001
contractor_0002
contractor_0003
contractor_0004
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.