2012-06-11 35 views
1

我在html文件中有一些報告。我需要將它們放在excel中並進行一些更改,所以我想我可以事先使用powershell進行這些更改。其中一些行位於固定位置,其他位置不是這樣,所以我需要通過使腳本識別圖案來刪除它們。Powershell,從html文件中刪除文本行

固定線從頂部開始: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來生成接近原始文件大小的文件,但在某些部分中打破了文本編碼。

我不知道如何去爲雖然承認格局...

回答

0

輸出文件的文件大小是原來的兩倍,因爲原始文件可能是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 
+0

這對固定線刪除很好:)關於模式我可以做一些像(gc $ maindir \ Report23.HTML)|其中{$ _ -notmatch'。*'n 。* ...'} ...? – kokotas

+0

我最終創建了一個excel宏,但我會將您的答案標記爲已接受的答案,因爲它涵蓋了我嘗試實現的大部分內容。儘管只有一個修正:$ rangefrombottom =($ length-59)..($ length-47)+($ length-12)..($ length-2) – kokotas

0

你能不能做這樣的事情:

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

,然後使用該數組的WHERE子句中:

? {$lines -notcontains $_.ReadCount} 
+0

哼不,它只替換12-14。 – kokotas