gpt4 book ai didi

sed 从文件中删除 URL

转载 作者:行者123 更新时间:2023-12-03 14:19:50 24 4
gpt4 key购买 nike

我正在尝试编写一个可以从文件中删除 url 的 sed 表达式

例子

http://samgovephotography.blogspot.com/ updated my blog just a little bit ago. Take a chance to check out my latest work. Hope all is well:)   

Meet Former Child Star & Author Melissa Gilbert 6/15/09 at LA's B&N https://hollywoodmomblog.com/?p=2442 Thx to HMB Contributor @kdpartak :)

但我不明白:
sed 's/[\w \W \s]*http[s]*:\/\/\([\w \W]\)\+[\w \W \s]*/ /g' posFile  

固定的!!!!!

处理几乎所有情况,甚至是格式错误的 URL
sed 's/[\w \W \s]*http[s]*[a-zA-Z0-9 : \. \/ ; % " \W]*/ /g' positiveTweets | grep "http" | more

最佳答案

以下删除 http://https://以及直到下一个空间的所有内容:

sed -e 's!http\(s\)\{0,1\}://[^[:space:]]*!!g' posFile  
updated my blog just a little bit ago. Take a chance to check out my latest work. Hope all is well:)

Meet Former Child Star & Author Melissa Gilbert 6/15/09 at LA's B&N Thx to HMB Contributor @kdpartak :)

编辑:

我应该使用:
sed -e 's!http[s]\?://\S*!!g' posFile

与“ [s]\?”相比,“ s”是一种更易读的“可选 \(s\)\{0,1\}”方式

\S* ”是“任何非空格字符”的比“ [^[:space:]]* ”更具可读性的版本

我一定一直在使用 sed在我写这个答案时我的 Mac 安装了它 ( brew install gnu-sed FTW)。

那里有更好的 URL 正则表达式(例如,那些考虑了 HTTP(S) 以外的方案的那些),但是鉴于您提供的示例,这对您有用。为什么要把事情复杂化?

关于sed 从文件中删除 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4283344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com