2012-12-15 21 views
-1

为了,我必须:
1)抓住从txt文件
PowerShell的得到.txt文件,抢源链接,得到的字符串,导出为CSV

http://example1.htm
HTTP各个环节:// example2.htm
HTTP://example3.htm
...

2)从每一个环节
3)获得源从源
让我的琴弦4)将字符串导出到csv

它可以使用一个链接。例如:

$topic1 = "kh_header.><b>((?<=)[^<]+(?=</b>))" 
$topic2 = "<b>Numer ogłoszenia:\s([^;]+(?=;))" 
Select-String -Path strona1.htm -pattern $topic1 | foreach-object { 
$_.line -match $topic1 > $nul 
$out1 = $matches[1] 
} 
Select-String -Path strona1.htm -pattern $topic2 | foreach-object { 
$_.line -match $topic2 > $nul 
$out2 = $matches[1] 
} 
echo $out1';'$out2';' | Set-content out.csv -force 

,但我不能得到它与txt文件中的很多链接。我试试看:

$topic = "kh_header.><b>((?<=)[^<]+(?=</b>))" 
$topic2 = "<b>Numer ogłoszenia:\s([^;]+(?=;))" 
$folder = Get-ChildItem e:\sk\html 
    ForEach ($htmfile in $folder){ 
    If ($_.extension -eq ".htm"){ 
    $htmfile = ForEach-Object { 
      $WC = New-Object net.webclient 
      $HTMLCode = $WC.Downloadstring($_.fullname) 
      } 
     Select-String -Path $HTMLCode -pattern $topic | foreach-object { 
     $_.line -match $topic > $nul 
     $out1 = $matches[1] 
     }  
     Select-String -Path $HTMLCode -pattern $topic2 | foreach-object { 
     $_.line -match $topic2 > $nul 
     $out2 = $matches[1] 
     }  
     echo $out1';'$out2';' | Set-content out.csv -force  
    } 
} 

我该如何得到它?

+0

完整的源代码示例工作的任何最终解决方案关于它 ? – Kiquenet

回答

1

当您在默认情况下使用Select-String时,它只会在任何特定行上找到第一个匹配项。您可以使用AllMatches参数来解决这个问题例如为:

foo.txt contains: "static void Main(string[] args)" 

Select-String foo.txt -pattern '\W([sS]..)' -AllMatches | 
    Foreach {$_.Matches} | 
    Foreach {$_.Groups[1].Value} 

此外,Select-String会是面向行的,所以它不会发现模式跨行匹配。为了找到这些,你需要的文件中读作串串如:

$text = [io.file]::readalltext("$pwd\foo.txt") 

然后使用一些特殊的正则表达式的指令如:

$text | Select-String -pattern '(?si)\W([sS]..)' -AllMatches | 
     Foreach {$_.Matches} | 
     Foreach {$_.Groups[1].Value} 
+0

'$ folder = Get-ChildItem e:\ sk \ html \ $ out = Select-String -Path $ folder -pattern $ topic1,$ topic2 -AllMatches | foreach {$ _。Matches} | foreach {$ _。Groups [1] .Value} $ out | format-table value -auto $ out | select *,@ {N ='Value'; E = {$ _}} | ConvertTo-csv | out-file s5.csv -Force'
如何获取';'在$ topic1和$ topic2之间?在这个例子中,脚本获取输出文件中每一行的每一个匹配。如何获得简单的记录列? –

+0

我的意思是:带有(topic1,topic2,topic3)的简单列并将匹配的字符串记录到这些列中 –

+0

捕获组是根据'()'在正则表达式中的位置进行编号的。捕获组的编号(从1开始)对应于$ _。Groups [n] .Value中的编号。摆弄'n',以及如何在你的正则表达式中使用捕获组。 –