2017-08-10 47 views
1

我有2個CSV文件。兩者都包含連接到2個網站的所有URL。Bash/Python比較2 CSV文件輸出到.htaccess重定向

1是活的,第二個是在開發中。

我目前面臨的問題是,網站#2的網址格式有點不同,所以爲了SEO的利益,我需要生成一堆301 HTaccess重定向,比較來自2個CSV文件的網址。

我並不太在意.htaccess的輸出太多,因爲我總是可以在事後追加redirect的內容,但是我怎樣才能比較2個CSV,並且如果CSV1中的URL是LIKE那麼URL在CSV2,輸出線到3號的文件中:

URL1 URL

格式的類型?

例如:

CSV1包含:

http://url1/the-page-1 
http://url1/the-page-2 
http://url1/the-page-3 
http://url1/the-page-4 

CSV2包含:

http://url2/someplace/the-page-1 
http://url2//someotherplace/the-page-2 
http://url2/the-page-3 
http://url2/andyetanotherplace/the-page-4 

,並輸出到:

http://url1/the-page-1 http://url2/someplace/the-page-1 
http://url1/the-page-2 http://url2//someotherplace/the-page-2 
http://url1/the-page-3 http://url2/the-page-3 
http://url1/the-page-4 http://url2/andyetanotherplace/the-page-4 

的真實數據,並awk -F/ 'NR == FNR {a[$NF]=$0; next} $NF in a {print a[$NF], $0 > "combined.csv"}' old-site.csv new-site.csv OUTPUT被上傳到:Upload

回答

2

您可以使用awk此:

awk 'BEGIN{FS=OFS="/"} {gsub(/\/$/, ""); $NF=tolower($NF)} NR==FNR{a[$NF]=$0; next} 
    $NF in a {print a[$NF] " " $0 > "combined.csv"}' old-site.csv new-site.csv 


cat combined.csv 

http://url1/the-page-1 http://url2/someplace/the-page-1 
http://url1/the-page-2 http://url2//someotherplace/the-page-2 
http://url1/the-page-3 http://url2/the-page-3 
http://url1/the-page-4 http://url2/andyetanotherplace/the-page-4 

參考:Effective AWK Programming

+0

任何想法如何,我現在可以刪除匹配的鏈接舊網站和新網站?或者我應該這樣做,作爲一個新的問題;) – Kevin

+1

是的請發表一個新的問題。 – anubhava