使用正则表达式修剪网址?

时间:2014-11-17 19:22:14

标签: 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.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

这可能与正则表达式有关,还是我应该寻找不同的方法呢?

我希望通过命令行来完成它,但是我可以通过像NotePad ++那样运行它,如果这更容易......

3 个答案:

答案 0 :(得分:1)

试试这个:

sed 's@/[^/]\+$@@g' file

答案 1 :(得分:1)

使用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

答案 2 :(得分:1)

awk NF-- FS=/ OFS=/

结果

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