【问题标题】:Powershell convert tabbed text file content to well formatted HTML TablePowershell 将选项卡式文本文件内容转换为格式良好的 HTML 表
【发布时间】:2018-07-01 10:17:50
【问题描述】:

我想在powershell中将一个文本文件处理成格式良好的HTML表格,下面是文件内容-

Machine ID  Proc Name   Proc Inst  Status         Comment             
----------------------------------------------------------------------
1           BG          1          RUNNING        BG process started  
1           BG          2          RUNNING        BG process started  
1           BG          3          RUNNING        BG process started  
1           BGPREDICT   1          RUNNING        BG process started  
1           DLMGR       1          RUNNING        DLMGR process started
1           IAPJMS      1          NOT RUNNING                        
1           RPCBG       1          RUNNING        RPCBG process started
1           RPC_TCP_LI  1          RUNNING        RPC listener process started
1           RPC_UDP_LI  1          RUNNING        RPC listener process started
1           SPO         1          RUNNING        SPO Server process started
1           SPO         2          RUNNING        SPO Server process started
1           WIS         1          RUNNING        WIS process started 
1           WIS         2          RUNNING        WIS process started 
1           WIS         3          RUNNING        WIS process started 
1           WIS         4          RUNNING        WIS process started 
1           WIS         5          RUNNING        WIS process started 
1           WIS         6          RUNNING        WIS process started 
1           WISMBD      1          RUNNING        WISMBD process started
1           WISMBD      2          RUNNING        WISMBD process started
1           WQS         1          RUNNING        WQS process started 


Timed out waiting for system status response.

我无法转换,因为它们看起来与 HTML 表格形式相同,但通过使用下面的 sn-p- 将所有内容放在每行的单个单元格中-

Get-Content $env:TEMP\stat.txt|ConvertTo-HTML -Property
   @{Label='Text';Expression={$_}}

来自文本文件的标题应与其余的 HTML 表格中的相同,所有应分别与每个单元格一样对待

请帮助我实现这一目标。

【问题讨论】:

  • 您首先必须将文本文件拆分为适当的列,我看到的唯一共同点是拆分多个空格,即使使用空尾列也很难做到这一点。

标签: powershell powershell-2.0


【解决方案1】:

此脚本使用多个正则表达式替换解析成一个对象以形成一个 csv,然后使用 ConvertFrom-Csv cmdlet:

$table= (gc .\sample.txt -raw ) -replace "-{10,100}`r?`n" -replace "`r?`n *`r?`n"
$table -split "`r?`n" | %{
    If ($_ -notmatch 'Timed out'){
        "`"{0}`"" -f ($_ -replace " {2,15}",'","')
    }
}|ConvertFrom-Csv | ConvertTo-html | Set-Content .\Sample.html -Encoding UTF8

样本ConvertFrom-Csvoutput

Machine ID Proc Name  Proc Inst Status      Comment
---------- ---------  --------- ------      -------
1          BG         1         RUNNING     BG process started
1          BG         2         RUNNING     BG process started
...snip..

通过管道传送到ConvertTo-html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/><col/><col/></colgroup>
<tr><th>Machine ID</th><th>Proc Name</th><th>Proc Inst</th><th>Status</th><th>Comment</th></tr>
<tr><td>1</td><td>BG</td><td>1</td><td>RUNNING</td><td>BG process started</td></tr>
<tr><td>1</td><td>BG</td><td>2</td><td>RUNNING</td><td>BG process started</td></tr>
...snip...

并保存到您可以在浏览器中查看的文件中:

【讨论】:

  • 感谢您的代码,但我使用的是 PS 2.0,其中 -raw,替换为 GC 不起作用 -PSVersion 2.0,如果您可以提供任何替代方式吗?
  • 你为什么不首先使用powershell-v2.0标签?现在去睡觉了,evtl我明天去看看。
  • 你能帮我换个逻辑吗?
  • 抱歉,没有。从来没有对 v2 限制有过更深入的体验,并且应该总是有一台最新的带有 Windows10 的 PC。也许你最好删除这个问题并再次提出 PSv2 并将 csv 作为文本而不是图像发布。
  • 这个小sn-p 帮助我完成了一项不相关但相似的任务。我不知道该怎么感谢你才足够! @LotPings
猜你喜欢
  • 2012-07-20
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
相关资源
最近更新 更多