有人可以帮我简化正则表达式吗?

发布于 2024-12-09 15:15:06 字数 165 浏览 0 评论 0原文

我想 grep HTTP 访问日志,但我无法编写有效的正则表达式。这是我现在使用的:

grep \/console access.log | grep -v .gif | grep -v .js |grep -v .css

我怎样才能缩短它?谢谢!

I want to grep a HTTP access log, but I'm not able to write an efficient regular expression. Here is what I use now:

grep \/console access.log | grep -v .gif | grep -v .js |grep -v .css

How can I shorten it? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-12-16 15:15:06

grep 不支持前瞻,因此您仍然需要有两个实例:

grep /console access.log | grep -v '\.\(gif|js|css\)'

对于负前瞻,较小但不一定更具可读性的正则表达式将是

^(?!.*?\.(gif|js|css)).*/console.*$

grep does not support lookahead, so you'll still have to have two instances:

grep /console access.log | grep -v '\.\(gif|js|css\)'

With negative lookahead, a smaller, although not necessarily more readable regexp would be

^(?!.*?\.(gif|js|css)).*/console.*$
青萝楚歌 2024-12-16 15:15:06

这可能感觉更简单,也可能不更简单。

sed -r -n -e '/\.(js|css|gif)/d' -e '\%/console%p' access.log

This may or may not feel simpler.

sed -r -n -e '/\.(js|css|gif)/d' -e '\%/console%p' access.log
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文