簡體   English   中英

Powershell,從html文件中刪除文本行

[英]Powershell, delete lines of text from html file

我在html文件中有一些報告。 我需要將它們放置在卓越的位置並進行一些更改,所以我認為我可以事先使用powershell進行這些更改。 有些行位於固定位置,而其他行則不在,因此我需要通過使腳本識別模式來刪除它們。

從頂部開始的固定線:12-14,17,19,25-27,30-32,40-42從底部開始的固定線:3-13,48-60

我需要查找和刪除的模式是這樣的:

<td align="center">random string</td>
<td align="left">random string</td>
<td align="left">random string</td>
<td align="left">random string</td>
<td align="right">random string</td>

對於固定線路,我可以執行以下操作:

(gc $maindir\Report23.HTML) | ? {(12..14) -notcontains $_.ReadCount} | out-file $maindir\Report23b.HTML

它在刪除第12-14行時起作用,但是我需要將其余的固定行號放在同一命令中,我似乎無法弄清楚該怎么做。 另外,輸出文件的文件大小是原始文件大小的兩倍,我覺得很奇怪。 我嘗試使用set-content生成接近原始文件的文件大小,但在某些部分中斷了文本編碼。

我不知道如何去識別模式...

你不能做這樣的事情:

$lines = 12..14
$lines += 17
$lines += 25..27
$lines += 30..32
$lines += 40..42

然后在where子句中使用該數組:

? {$lines -notcontains $_.ReadCount} 

輸出文件的文件大小是原始文件的兩倍,因為原始文件可能是ASCII編碼的,新文件默認是Unicode編碼的。 嘗試這個:

$length = (gc $maindir\Report23.HTML).length
$rangefrombottom = ($length-60)..($length-48)+($length-13)..($length-3)
$rangefromtop = 12..14+17,19+25..27+30..32+40..42
(gc $maindir\Report23.HTML) | ? {$rangefromtop -notcontains $_.ReadCount} | ? {$rangefrombottom -notcontains $_.ReadCount} | out-file -encoding ASCII $maindir\Report23b.HTML

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM