我有一个输入txt文件,其中包含不同的数据:
------------------------------------------------------------------------
Current Local Time: Fri Jul 01 04:54:27 2016
Current GMT Time: Thu Jun 30 20:54:27 2016
Machine ID: 6090
Machine Name: WL6090
Display S/N: 0253G020TG
Terrain version: 5.3.843.843
CAESTaskList DLL version: 5.3.843.843
Application Type: Terrain / CAES Ultra
Operating System: 1.04
Total RAM used by active files: 339.72 kB
----------------------------------------------------------------------------------------------------------
Disk Space
Free Disk Space: 6758 MB Total Disk Space: 7076 MB
----------------------------------------------------------------------------------------------------------
Current Local Time: 07-01-16, 04:54:27
Current Service Hours: 314.41
我每天都有很多这样的文件。 我尝试使用powershell作为一个真正的新手,只从这些文件中获取我需要的信息,我这样做:
$Location = "D:\terrain_dia\Diag01Jul2016_045427.dia"
$a = Get-Content D:\terrain_dia\Diag01Jul2016_045427.dia
#Common
$option = [System.StringSplitOptions]::RemoveEmptyEntries
#ID
$LineID = Select-String -Path $Location -Pattern "Machine ID:" | Select-Object -Expand LineNumber
$ID = ($a)[$LineID-1]
$ID -replace '(?:\s|\r|\n)',''
#Name
$LineName = Select-String -Path $Location -Pattern "Machine Name:" | Select-Object -Expand LineNumber
$Name = ($a)[$LineName-1]
$Name -replace '(?:\s|\r|\n)',''
#SN
$LineSN = Select-String -Path $Location -Pattern "Display S/N:" | Select-Object -Expand LineNumber
$SN = ($a)[$LineSN-1]
$SN -replace '(?:\s|\r|\n)',''
#Version
$LineVersion = Select-String -Path $Location -Pattern "Terrain version:" | Select-Object -Expand LineNumber
$Version = ($a)[$LineVersion-1]
$Version -replace '(?:\s|\r|\n)',''
#RAM
$LineRam = Select-String -Path $Location -Pattern "Total RAM" | Select-Object -Expand LineNumber
$RAM = ($a)[$LineRam-1]
$RAM -replace '(?:\s|\r|\n)',''
#GPSSN
$LineGPSSN = Select-String -Path $Location -Pattern "GPS Receiver SN:" | Select-Object -Expand LineNumber
$GPSSN = ($a)[$LineGPSSN-1]
$GPSSN -replace '(?:\s|\r|\n)',''
#GPSType
$LineGPSType = Select-String -Path $Location -Pattern "GPS Receiver Type:" | Select-Object -Expand LineNumber
$GPSType = ($a)[$LineGPSType-1]
$GPSType -replace '(?:\s|\r|\n)',''
#NAV
$LineNAV = Select-String -Path $Location -Pattern "NAV firmware version:" | Select-Object -Expand LineNumber
$NAV = ($a)[$LineNAV-1]
$NAV -replace '(?:\s|\r|\n)',''
#SIG
$LineSig = Select-String -Path $Location -Pattern "SIG firmware version:" | Select-Object -Expand LineNumber
$SIG = ($a)[$LineSig-1]
$SIG -replace '(?:\s|\r|\n)',''
#ROM
$LineROM = Select-String -Path $Location -Pattern "ROM firmware version:" | Select-Object -Expand LineNumber
$ROM = ($a)[$LineROM-1]
$ROM -replace '(?:\s|\r|\n)',''
我得到了输出:
MachineID:6090
MachineName:WL6090
DisplayS/N:0253G020TG
Terrainversion:5.3.843.843
TotalRAMusedbyactivefiles:339.72kB
GPSReceiverSN:3351J508SP
GPSReceiverType:MS992
NAVfirmwareversion:00506
SIGfirmwareversion:00506
ROMfirmwareversion:00425
FreeDiskSpace:6758MB
TotalDiskSpace:7076MB
所以我有一个固定的分隔符 但我真正需要的是:
MachineID MachineName DisplayS/N
6090 WL6090 0253G020TG
等
所以我需要将此输出格式化为txt文件中的表格,然后我将把结果放入MS SQL数据库中。
请告知我如何使用PowerShell格式化所有这些行?
任何帮助都非常有用。
谢谢
答案 0 :(得分:0)
解析这样的文档可能有点烦人,但上面的内容非常简单。
$InputFile.replace("MB","MB`n") -split "\n" |
Select-String ":" | % {
$_.Line.Trim() -replace ":\s+","`t"
} | ConvertFrom-Csv -Delimiter "`t" -Header "Detail","Value"
这将输入作为字符串,然后:
MB
值之后添加换行符,因为一行中有多个换行符。:
的行,以排除垃圾和空格。:____
部分替换为标签。结果:
Detail Value
------ -----
Current Local Time Fri Jul 01 04:54:27 2016
Current GMT Time Thu Jun 30 20:54:27 2016
Machine ID 6090
etc...
您可以使用以下内容获得长格式输出:
$c.Detail -join "`t"
$c.Value -join "`t"
希望这有帮助,我已经使用了代码(不包括MB
- > MB\n
部分)来解析类似的日志文件几次。
答案 1 :(得分:0)
这个衬里将处理当前文件夹中的所有* .dia文件,并使用RegEx过滤包含冒号的所有行并构建压缩的名称值对并显示格式化:
Select-String .\*.dia -Pattern '^([^:]+):\s+(.*)$' | ForEach-Object { "{0,-20} {1,-25}: {2}" -f $_.FileName,$($_.Matches.Groups[1].Value -replace ' '), ($_.Matches.Groups[2].Value) }
示例输出:
Sample1.dia CurrentLocalTime : Fri Jul 01 04:54:27 2016
Sample1.dia CurrentGMTTime : Thu Jun 30 20:54:27 2016
Sample1.dia MachineID : 6090
Sample1.dia MachineName : WL6090
Sample1.dia DisplayS/N : 0253G020TG
Sample1.dia Terrainversion : 5.3.843.843
Sample1.dia CAESTaskListDLLversion : 5.3.843.843
Sample1.dia ApplicationType : Terrain / CAES Ultra
以下PowerShell脚本使用相同的RegEx并构建[PSCustomObject]并将所有属性附加到它(此时出现错误,CurrentLocalTime
在文件中出现两次,TotalDiskSpace与FreeDiskSpace位于同一行)
否则,它会累积它在文件中找到的所有数据CumulatedDia.Csv
保存并重新导入以通过Out-GridView
显示内容。我留下了两个缺陷,由您/其他人修复。
$CumDia = ".\CumulatedDia.Csv"
Remove-Item $CumDia -ErrorAction Ignore
ForEach($File IN (Get-ChildItem *.dia)){
$DiaFile = $Null
$DiaFile = [PSCustomObject]@{'FileOrigin' = $File}
Select-String -Path $File -Pattern '^([^:]+):\s+(.*)$' | ForEach-Object {
$DiaFile | Add-Member -EA SilentlyContinue `
-NotePropertyName ($_.Matches.Groups[1].Value -replace ' ')`
-NotePropertyValue $_.Matches.Groups[2].Value
}
$DiaFile | fl
$DiaFile | Export-Csv -Path $CumDia -Append -NoTypeInformation
}
Import-Csv $CumDia|Out-GridView
可以使用Select-Object删除不需要的属性/列。 这是一行表
的Format-List输出FileOrigin : Q:\Test\2017\07\25\Sample1.dia
CurrentLocalTime : Fri Jul 01 04:54:27 2016
CurrentGMTTime : Thu Jun 30 20:54:27 2016
MachineID : 6090
MachineName : WL6090
DisplayS/N : 0253G020TG
Terrainversion : 5.3.843.843
CAESTaskListDLLversion : 5.3.843.843
ApplicationType : Terrain / CAES Ultra
OperatingSystem : 1.04
TotalRAMusedbyactivefiles : 339.72 kB
FreeDiskSpace : 6758 MB Total Disk Space: 7076 MB
CurrentServiceHours : 314.41
GPSReceiverSN : 3351J508SP
GPSReceiverType : MS992
NAVfirmwareversion : 00506
SIGfirmwareversion : 00506
ROMfirmwareversion : 00425