Regex 使用正则表达式修剪URL?

Regex 使用正则表达式修剪URL?,regex,shell,command-line,ssh,command,Regex,Shell,Command Line,Ssh,Command,我有一个遵循此格式的文本文件: http://www.mywebsite.com/user1242/random-information http://www.mywebsite.com/otheruser/writing-other-stuff http://www.mywebsite.com/anotheruser/fwefawef-wfefwef http://www.mywebsite.com/testuser/fwefweaf-fawefw http://www.mywebsite.c

我有一个遵循此格式的文本文件:

http://www.mywebsite.com/user1242/random-information
http://www.mywebsite.com/otheruser/writing-other-stuff
http://www.mywebsite.com/anotheruser/fwefawef-wfefwef
http://www.mywebsite.com/testuser/fwefweaf-fawefw
http://www.mywebsite.com/testing123/this-is-a-test
我想修剪最后一部分,使列表如下所示:

http://www.mywebsite.com/user1242
http://www.mywebsite.com/otheruser
http://www.mywebsite.com/anotheruser
http://www.mywebsite.com/testuser
http://www.mywebsite.com/testing123
这是否可以用正则表达式实现,或者我应该寻找一种不同的方法来实现

我希望通过命令行来实现,但如果更简单的话,我可以通过记事本++之类的工具来实现…

试试这个:

sed 's@/[^/]\+$@@g' file
使用awk:

awk 'BEGIN{FS=OFS="/"} {$NF=""; print substr($0, 1, length($0)-1)}' file
http://www.mywebsite.com/user1242
http://www.mywebsite.com/otheruser
http://www.mywebsite.com/anotheruser
http://www.mywebsite.com/testuser
http://www.mywebsite.com/testing123
结果

http://www.mywebsite.com/user1242 http://www.mywebsite.com/otheruser http://www.mywebsite.com/anotheruser http://www.mywebsite.com/testuser http://www.mywebsite.com/testing123 http://www.mywebsite.com/user1242 http://www.mywebsite.com/otheruser http://www.mywebsite.com/anotheruser http://www.mywebsite.com/testuser http://www.mywebsite.com/testing123
工作完美!谢谢我会在8分钟内接受答案,如果它允许的话!既然你可以
NF--
和打印,为什么还要玩长度游戏呢?@EtanReisner:
NF--
只对gnu awk有效,对OSX上的BSD awk等其他awk不起作用
NF--
只对gnu awk有效,对OSX上的BSD awk等其他awk版本不起作用 http://www.mywebsite.com/user1242 http://www.mywebsite.com/otheruser http://www.mywebsite.com/anotheruser http://www.mywebsite.com/testuser http://www.mywebsite.com/testing123