regex - 使用正则表达式修剪URL?

标签 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 ++之类的工具来运行它。

最佳答案

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

关于regex - 使用正则表达式修剪URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26980301/

相关文章:

bash - 登录 shell 和交互式 shell 之间有什么区别?

linux - 单击选项卡时出现shell init问题,getcwd有什么问题?

windows - 如何更改 Windows 命令提示符中的文本颜色

node.js - NPM Start – 权限错误

php - 从 preg_replace 到 preg_replace_callback

regex - 如何检测字符串中的笑话?

shell - 在makefile中获取父目录的基本名称

javascript - JavaScript 中的正则表达式。只允许字母、逗号和标点符号

python - Python 中的正则表达式匹配

java - 如何判断 Java 中使用的输入类型