2011-09-07 28 views
1

我有像的鏈接:最後拆下後文/

http://site1.com/lalala.html 
http://site2.com/lalala/ 

我需要刪除所有文字最後一行後/用awk或sed的,怎麼會怎麼辦?或者,也許一些Perl或Python腳本?感謝幫助!

回答

2

這可以工作:

$ echo http://site1.com/lalala/ | sed -e 's;\(.*/\).*;\1;' 
http://site1.com/lalala/ 
$ echo http://site1.com/lalala/foo.html | sed -e 's;\(.*/\).*;\1;' 
http://site1.com/lalala/ 
+0

非常感謝,它的工作原理! – llokely

0

在PHP中,你會在字符串上使用dirname()。 PHP是可能的腳本語言之一嗎?

編輯:

Perl和Python都有dirname(),不是很確定的執行。遺憾

0

除了所使用的,我會分裂的所有行(與\ n的分離器),以陣列的語言,那麼我會爲lastIndexOf「/」執行搜索每個元素,然後我會刪除我不需要使用常規字符串方法的部分。當然你也可以用RegExp做一些有趣的事情。

0

您可以使用

sed -e 's/\(.*\)\/[^\/]*/\1/g' 

例子:

bash-$ echo "http://site1.com/lalala.html" |sed -e 's/\(.*\)\/[^\/]*/\1/g' 
http://site1.com 
bash-$ echo "http://site2.com/lalala/" |sed -e 's/\(.*\)\/[^\/]*/\1/g' 
http://site2.com/lalala 
0

用awk可以使用:

awk 'BEGIN { FS="/"; OFS="/"; } { $NF = ""; print; }' 

例子:

$ echo http://site1.com/lala/la.txt | awk 'BEGIN { FS="/"; OFS="/"; } { $NF = ""; print; }' 
http://site1.com/lala/ 

$ echo http://site1.com/lala/ | awk 'BEGIN { FS="/"; OFS="/"; } { $NF = ""; print; }' 
http://site1.com/lala/