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

标签 python bash .htaccess csv redirect

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

第一个已上线,第二个正在开发中。

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

我并不太担心 .htaccess 输出,因为我总是可以附加 redirect事后的事情,但我如何比较 2 个 CSV,如果 CSV1 中的 URL 是 LIKE CSV2 中的 URL,并将该行输出到 a 中的第三个文件:

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 的输出已上传至:Upload

最佳答案

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

关于python - Bash/Python 比较 2 个 CSV 文件输出到 .htaccess 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45616698/

相关文章:

bash - 转换 mp3 -> 带有静态图像的视频 (ffmpeg/libav & BASH)

bash - 默认情况下,bash 是否在/usr/local/etc/bash_completion.d 中生成 bash 完成文件?

regex - Bash 脚本正则表达式

regex - 如何使用 htaccess 将多个域重定向到除 1 个目录之外的另一个域?

python - 如何从函数中截取实例化的类名?

python - 如何处理列名和创建新列

apache - tld 中断来自外部链接的 ssl 重定向后缺少尾部斜线

apache - ht仅访问子文件夹中的一个文件并保留url

Python Tkinter 滚动条不稳定滚动

python - 不平衡面板数据 : How to use Time Series Splits Cross-Validation?