首页 文章

Bash / Python比较2个CSV文件输出到.htaccess重定向

提问于
浏览
1

我有2个CSV文件 . 两者都包含附加到2个网站的所有URL .

1是现场,第二是正在开发中 .

我目前面临的问题是,网站#2的网址格式有点不同,所以为了SEO,我需要生成一堆301 HTaccess重定向,比较2个CSV文件中的URL .

我并不是真的担心.htaccess输出,因为我总是可以在事后附加 redirect 的东西,但我如何比较2个CSV,如果CSV1中的URL是 LIKE CSV2中的URL,并且将该行输出到以下位置的第3个文件:

URL1 URL

格式类型?

For instance:

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 的REAL DATA和OUTPUT上传到:Upload

1 回答

  • 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
    

    Reference: Effective AWK Programming

相关问题