2012-12-13 73 views
4

使用SED我有一個問題想創造一個sitemap.xml的SED比賽日期

# 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 

之前匹配和刪除一個txt文件博客條目我無法弄清楚如何搭配年/月份。我想刪除包含所有行「.TLD /年/月/」

我知道$ hostTLD一部分工作,因爲我使用它使用不同的匹配:

sed -i '/\.'"${hostTLD}"'\/category\//d' filename # works! ".TLD/category/" 

回答

1

你接近,但是您需要在您的sed命令周圍使用雙引號,並使用大括號。試試這個:

sed -i "/\.$hostTLD\/[0-9]\{4\}\/[0-9]\{2\}/d" filename 

關於第二個命令,使用此:

sed -i "/\.$hostTLD\/category\//d" filename 
+1

這完美地工作。謝謝! – Dan