我正在创建一个Powershell脚本来格式化.txt文件并最终将其转换为csv文件(除非可以一起完成)。我已经研究了很多其他问题,但是还没有弄清楚。每次我在下面运行脚本时,它都会选择我需要的所有数据,但将它们全部放在一行上。
$importPath = 'C:\Example.txt'
$pattern = "<return>(.*?)</return>"
$string = Get-Content $importPath
$result = [regex]::match($string, $pattern).Groups[1].Value
$result | Out-File -FilePath 'C:\ResponseOnly.txt'
在提供一种方法来获取<return>
和</return>
之间的所有信息的同时,仍然希望在原始行上分解所有数据的过程中,我将不胜感激。
源文件示例:
Status: OK
Time Taken: 193
Size: 46511
Timestamp: Thu May 30 03:39:37 MDT 2019
TestStep: getReportResultCsv
----------------- Messages ------------------------------
----------------- Properties ------------------------------
Endpoint: XXXXXXXXXXXXXXXXXXXXXXX
Username: XXXXXXXXXXXXXXXXXXXXXXX
domain: XXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1
Encoding: UTF-8
Password: XXXXXXXXXXXXXXXXXXXXXXX
---------------- Request ---------------------------
Request Headers: SOAPAction : ""
Authorization : Basic XXXXXXXXXXXXXXXXXXXXXXX
Connection : Keep-Alive
User-Agent : Apache-HttpClient/4.1.1 (java 1.5)
Host : XXXXXXXXXXXXXXXXXXXXXXX
Accept-Encoding : gzip,deflate
Content-Length : 378
Content-Type : text/xml;charset=UTF-8
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="XXXXXXXXXXXXXXXXXXXXXXX">
<soapenv:Header/>
<soapenv:Body>
<ser:getReportResultCsv>
<!--Optional:-->
<identifier>A0294584B2F933513rt0.c.ie.oF0BBB@5p70B7FsBlEf4v490cAm</identifier>
</ser:getReportResultCsv>
</soapenv:Body>
</soapenv:Envelope>
---------------- Response --------------------------
Response Headers: Transfer-Encoding : chunked
Server : nginx
Access-Control-Allow-Origin : *
Access-Control-Allow-Methods : POST,GET,OPTIONS
Connection : keep-alive
Content-Encoding : gzip
Set-Cookie : clientId=XXXXXXXXXXXXXXXXXXXXXXX
#status# : HTTP/1.1 200 OK
Date : Thu, 30 May 2019 09:39:37 GMT
Content-Type : text/xml;charset=UTF-8
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns2:getReportResultCsvResponse xmlns:ns2="XXXXXXXXXXXXXXXXXXXXXXX">
<return>DATE,AGENT GROUP,AGENT NAME,CALLS,HANDLE TIME,Average HANDLE TIME,HOLD TIME,Average HOLD TIME,Average AFTER CALL WORK TIME
2019/04/04,Billy Bob,Joe Joseph,6,04:15:53,00:42:39,00:00:00,00:00:00,00:01:55
2019/04/05,Billy Bob,Joe Joseph,14,04:39:26,00:19:58,00:47:53,00:03:25,00:02:20
2019/04/06,Billy Bob,Joe Joseph,11,06:33:13,00:35:45,00:17:21,00:01:35,00:04:30
2019/04/07,Billy Bob,Joe Joseph,13,05:47:06,00:26:42,00:02:03,00:00:09,00:02:27
2019/04/08,Billy Bob,Joe Joseph,11,03:20:21,00:18:13,00:00:03,00:00:00,00:02:44</return>
</ns2:getReportResultCsvResponse>
</env:Body>
</env:Envelope>
文件应为的示例:
DATE,AGENT GROUP,AGENT NAME,CALLS,HANDLE TIME,Average HANDLE TIME,HOLD TIME,Average HOLD TIME,Average AFTER CALL WORK TIME
2019/04/04,Billy Bob,Joe Joseph,6,04:15:53,00:42:39,00:00:00,00:00:00,00:01:55
2019/04/05,Billy Bob,Joe Joseph,14,04:39:26,00:19:58,00:47:53,00:03:25,00:02:20
2019/04/06,Billy Bob,Joe Joseph,11,06:33:13,00:35:45,00:17:21,00:01:35,00:04:30
2019/04/07,Billy Bob,Joe Joseph,13,05:47:06,00:26:42,00:02:03,00:00:09,00:02:27
2019/04/08,Billy Bob,Joe Joseph,11,03:20:21,00:18:13,00:00:03,00:00:00,00:02:44
答案 0 :(得分:1)
_ [
编辑-我错过了TheMadTechnician的帖子。 [叹息... ]我将把它留在这里,以防微小的差异对OP有所帮助。
编辑2-TheMadTechnician指出,在这种情况下,不需要为-Raw
使用Get-Content
参数。删除了它,并添加了必要的-split
以制作正确的伪造Get-Content
数据集。
] _
这是一种将数据导入CSV的方法。由于它是有效的XML,因此可以使用[xml]
类型的加速器将其从文本转换为XML对象。那么您可以将XML导航到csv数据所在的位置。到达那里后,可以将其馈送到ConvertFrom-CSV
cmdlet中以创建对象数组。
然后,最终 [咧嘴],您可以使用Export-CSV
将该整洁的数组导出到CSV文件。
这是代码...
# fake reading in a text file
# in real life, use Get-Content
$InStuff = @'
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns2:getReportResultCsvResponse xmlns:ns2="XXXXXXXXXXXXXXXXXXXXXXXXXXXXX">
<return>DATE,AGENT GROUP,AGENT NAME,CALLS,HANDLE TIME,Average HANDLE TIME,HOLD TIME,Average HOLD TIME,Average AFTER CALL WORK TIME
2019/04/04,Billy Bob,Joe Joseph,6,04:15:53,00:42:39,00:00:00,00:00:00,00:01:55
2019/04/05,Billy Bob,Joe Joseph,14,04:39:26,00:19:58,00:47:53,00:03:25,00:02:20
2019/04/06,Billy Bob,Joe Joseph,11,06:33:13,00:35:45,00:17:21,00:01:35,00:04:30
2019/04/07,Billy Bob,Joe Joseph,13,05:47:06,00:26:42,00:02:03,00:00:09,00:02:27
2019/04/08,Billy Bob,Joe Joseph,11,03:20:21,00:18:13,00:00:03,00:00:00,00:02:44</return>
</ns2:getReportResultCsvResponse>
</env:Body>
</env:Envelope>
'@ -split [System.Environment]::NewLine
$IS_XML = [xml]$InStuff
$ReportResult = $IS_XML.Envelope.Body.getReportResultCsvResponse.return |
ConvertFrom-Csv
# on screen
$ReportResult
# to CSV
$ReportResult |
Export-Csv -LiteralPath "$env:TEMP\TaylorGreen_-_ReportResult.csv" -NoTypeInformation
截屏输出...
DATE : 2019/04/04
AGENT GROUP : Billy Bob
AGENT NAME : Joe Joseph
CALLS : 6
HANDLE TIME : 04:15:53
Average HANDLE TIME : 00:42:39
HOLD TIME : 00:00:00
Average HOLD TIME : 00:00:00
Average AFTER CALL WORK TIME : 00:01:55
[*...snip...*]
DATE : 2019/04/08
AGENT GROUP : Billy Bob
AGENT NAME : Joe Joseph
CALLS : 11
HANDLE TIME : 03:20:21
Average HANDLE TIME : 00:18:13
HOLD TIME : 00:00:03
Average HOLD TIME : 00:00:00
Average AFTER CALL WORK TIME : 00:02:44
csv文件的文件内容...
"DATE","AGENT GROUP","AGENT NAME","CALLS","HANDLE TIME","Average HANDLE TIME","HOLD TIME","Average HOLD TIME","Average AFTER CALL WORK TIME"
"2019/04/04","Billy Bob","Joe Joseph","6","04:15:53","00:42:39","00:00:00","00:00:00","00:01:55"
"2019/04/05","Billy Bob","Joe Joseph","14","04:39:26","00:19:58","00:47:53","00:03:25","00:02:20"
"2019/04/06","Billy Bob","Joe Joseph","11","06:33:13","00:35:45","00:17:21","00:01:35","00:04:30"
"2019/04/07","Billy Bob","Joe Joseph","13","05:47:06","00:26:42","00:02:03","00:00:09","00:02:27"
"2019/04/08","Billy Bob","Joe Joseph","11","03:20:21","00:18:13","00:00:03","00:00:00","00:02:44"
答案 1 :(得分:0)
这比您期望的要容易,因为输入的内容是XML。试试这个:
my_var != d
这将为您提供所需的东西。
如果您真的想使用RegEx,我将以多行字符串的形式读取整个文件,并与之匹配以获取输出。您只需要告诉RegEx,当您使用多行字符串时就会遇到这种情况。
$importPath = 'C:\Example.txt'
[xml]$DataIn=Get-Content $importPath
$DataIn.Envelope.Body.getReportResultCsvResponse.return| Out-File -FilePath 'C:\ResponseOnly.txt'