簡體   English   中英

Bash / Python比較2個CSV文件輸出到.htaccess重定向

[英]Bash/Python Compare 2 CSV Files Output to .htaccess Redirects

我有2個CSV文件。 兩者都包含附加到2個網站的所有URL。

一是現場直播,二是開發中。

我目前面臨的問題是,站點2的URL的格式設置有些不同,因此,為了SEO的利益,我需要生成一堆301 HTaccess重定向,將2個CSV文件中的URL進行比較。

我真的不擔心太擔心的.htaccess輸出,我可以永遠只是追加redirect事后的東西,但我怎么能比較2 CSV的,如果在CSV1的URL是LIKE在CSV2網址,將行輸出到第三個文件中:

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 awk -F/ 'NR == FNR {a[$NF]=$0; next} $NF in a {print a[$NF], $0 > "combined.csv"}' old-site.csv new-site.csv上傳到: 上傳

您可以為此使用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

參考: 有效的AWK編程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM