Text 删除最后一个之后的文本/

Text 删除最后一个之后的文本/,text,Text,我有如下链接列表: http://site1.com/lalala.html http://site2.com/lalala/ 我需要删除last/之后的所有行中的文本。我如何使用awk或sed来执行此操作?或者一些perl或python脚本?谢谢你的帮助 在PHP中,对字符串使用dirname()。PHP是一种可能的脚本语言吗 编辑: Perl和Python都有dirname(),但我对实现不太确定。抱歉这可能有效: $ echo http://site1.com/lalala/ | sed

我有如下链接列表:

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

我需要删除last/之后的所有行中的文本。我如何使用awk或sed来执行此操作?或者一些perl或python脚本?谢谢你的帮助

在PHP中,对字符串使用
dirname()
。PHP是一种可能的脚本语言吗

编辑:

Perl和Python都有
dirname()
,但我对实现不太确定。抱歉

这可能有效:

$ 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/

除了使用的语言之外,我会将所有行(以\n作为分隔符)拆分为一个数组,然后搜索每个元素的lastIndexOf“/”,然后使用常规字符串方法删除不需要的部分。当然,您也可以使用RegExp做一些有趣的事情。

您可以使用

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
$ 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/
使用awk,您可以使用:

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

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
$ 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/