使用sed我在创建sitemap.xml之前尝试匹配和删除txt文件中的博客条目时遇到问题
# Contents of filename:
# http://www.example.com/2008/10/article3.html
# http://www.example.com/2009/11/article7.html
#!/bin/bash
hostName="www.example.com"
hostTLD="$(echo ${hostName}|cut -d . -f 3)" # results in "com"
sed -i '/\.'"${hostTLD}"'\/\([0-9]{4}\)\/\([0-9]{2}\)/d' filename
我无法弄清楚如何匹配年/月位。我想删除包含“.TLD / year / month /”
的所有行我知道$ hostTLD部分有效,因为我正在使用不同的匹配:
sed -i '/\.'"${hostTLD}"'\/category\//d' filename # works! ".TLD/category/"
答案 0 :(得分:1)
你很接近,但是你需要在你的sed
命令周围使用双引号来逃避括号。试试这个:
sed -i "/\.$hostTLD\/[0-9]\{4\}\/[0-9]\{2\}/d" filename
对于第二个命令,请使用:
sed -i "/\.$hostTLD\/category\//d" filename