Powershell:解析结构化文本文件并保存到.CSV [英] Powershell: Parse a structured text file and save to .CSV

查看:384
本文介绍了Powershell:解析结构化文本文件并保存到.CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Powershell的新手。

I'm very new to Powershell. Only have been using it for about 2 weeks.

我有一个文件结构如下:

I have a file that is structured like this:


Service name: WSDL 
Service ID: 14234321885 
Service resolution path: /gman/wsdlUpdte 
Serivce endpoints: 
-------------------------------------------------------------------------------- 
Service name: DataService 
Service ID: 419434324305 
Service resolution path: /widgetDate_serv/WidgetDateServ 
Serivce endpoints:  
http://servername.company.com:1012/widgetDate_serv/WidgetDateServ
-------------------------------------------------------------------------------- 
Service name: SearchService 
Service ID: 393234543546 
Service resolution path: /ProxyServices/SearchService 
Serivce endpoints:  
http://servername.company.com:13010/Services/SearchService_5_0
http://servername2.company.com:13010/Services/SearchService_5_0
-------------------------------------------------------------------------------- 
Service name: Worker 
Service ID: 14187898547 
Service resolution path: /ProxyServices/Worker 
Serivce endpoints:  
http://servername.company.com:131009/Services/Worker/v9
--------------------------------------------------------------------------------

我想解析文件,并具有服务名称,服务ID,服务解析路径和服务

I'd like to parse the file and have Service name, Service ID, Service Resolution Path and Service Endpoints (which sometimes contain multiple or no values) in individual columms (CSV).

除了使用Get-Content和循环遍历文件外,我还不知道哪里可以开始。

Beyond using Get-Content and looping through the file, I have no idea even where to start.

任何帮助将不胜感激。
感谢

Any help will be appreciated. Thanks

推荐答案

试试:


  1. 以一个字符串形式读取文件内容

  2. 将它拆分为81个连字符

  3. 取最后一个数组项

  4. 为每个项目创建新对象

  1. Read the file content as one string
  2. Split it by 81 hyphens
  3. Split each splited item on the colon char and take the last array item
  4. Create new object for each item

$pattern = '-'*81  
$content = Get-Content D:\Scripts\Temp\p.txt | Out-String
$content.Split($pattern,[System.StringSplitOptions]::RemoveEmptyEntries) | Where-Object {$_ -match '\S'} | ForEach-Object {

$item = $_ -split "\s+`n" | Where-Object {$_}

    New-Object PSobject -Property @{
        Name=$item[0].Split(':')[-1].Trim()
        Id = $item[1].Split(':')[-1].Trim()
        ResolutionPath=$item[2].Split(':')[-1].Trim()
        Endpoints=$item[4..($item.Count)]
    } | Select-Object Name,Id,ResolutionPath,Endpoints
}


这篇关于Powershell:解析结构化文本文件并保存到.CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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