我有一個需要搜索的超過500個字符串的列表。 (如果這很重要的話,它們就是網址。)我有一個擁有超過1,000個網頁的網站。我想搜索每個網頁來查找每個鏈接到的URL。如何爲多個字符串搜索多個文件
回到我們的網站在Unix機器上的時候,我會用find和grep來寫一個shell腳本來完成這個任務,但現在我們在Windows機器上,所以這不是一個真正的選擇。我根本沒有使用PowerShell的經驗,但我懷疑這是我需要的。但是,我不知道如何開始。
理想情況下,我願與落得什麼是這樣的:
<filename 1>
<1st string found>
<2nd string found>
<3rd string found>
<filename 2>
<1st string found>
<2nd string found>
我並不需要知道的行號;我只需要知道哪些URL在哪些文件中。 (我們將把所有500個以上的目標網址移動到新的位置,因此我們將不得不手動更新1,000個以上網頁中的鏈接,這將是一個皇家的痛苦。)
想必邏輯會是這樣的:
for each file {
print the filename
for each string {
if string found in file {
print the string
}
}
}
我們不能直接進行查找/替換,因爲網頁位於內容管理系統中。我們所能做的就是定位哪些頁面需要更新(使用本地驅動器上的網頁的靜態副本),然後手動更新CMS中的各個頁面。
我希望這很容易做到,但是我完全不熟悉PowerShell意味着我不知道從哪裏開始。任何幫助將不勝感激!
更新
感謝Travis Plunk的幫助!根據他的回答,這裏是我將要使用的代碼的最終版本。
# Strings to search for
$strings = @(
'http://www.ourwebsite.com/directory/somefile.pdf'
'http://www.ourwebsite.com/otherdirectory/anotherfile.pdf'
'http://www.otherwebsite.com/directory/otherfile.pdf'
)
# Directory containing web site files
cd \OurWebDirectory
$results = @(foreach($string in $strings)
{
Write-Host "Searching files for $string"
# Excluding the images directory
dir . -Recurse -Exclude \imagedir | Select-String -SimpleMatch $string
}) | Sort-Object -Property path
$results | Group-Object -Property path | %{
"File: $($_.Name)"
$_.Group | %{"`t$($_.pattern)"}
}
所以,你刮最終用戶可見頁面(它看起來像什麼,即只有'body')還是完整的HTML內容本身? ((編輯:這很重要,因爲我們需要保存完整的HTML並在所有'href'字段中進行搜索))。 – gravity
[Findstr](https://technet.microsoft.com/en-us/library/bb490907.aspx)? – n00dl3
我有本地磁盤訪問HTML文件本身,所以不需要屏幕抓取或網絡爬行。 –