2012-06-11 39 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