使用 sed 脚本从文本文件中删除时间戳

发布于 2024-10-22 20:46:49 字数 3250 浏览 1 评论 0原文

127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/ HTTP/1.1" 200 169 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/.treeinfo HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/Fedora HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/Server HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/Client HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/RedHat HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/CentOS HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/SL HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/directory.yast HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/current/images/MANIFEST HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/images/daily/MANIFEST HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/boot/platform/i86xpv/kernel/unix HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/platform/i86xpv/kernel/unix HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/STARTUP/XNLOADER.SYS HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/images/xen/vmlinuz HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/images/boot.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/boot/boot.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/current/images/netboot/mini.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/install/images/boot.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/ HTTP/1.1" 200 169 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/.treeinfo HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
1    27.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/Fedora HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/Server HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/Client HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/RedHat HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/CentOS HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/SL HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/directory.yast HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/current/images/MANIFEST HTTP/1.1" 404 182 "-" "Python-urllib/2.6"

如何使用 sed 脚本从上述日志中删除日期 [08/Mar/2011:00:26:35 +0530] 像这样有很多实例。

127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/ HTTP/1.1" 200 169 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/.treeinfo HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/Fedora HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/Server HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/Client HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/RedHat HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/CentOS HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/SL HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/directory.yast HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/current/images/MANIFEST HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/images/daily/MANIFEST HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/boot/platform/i86xpv/kernel/unix HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/platform/i86xpv/kernel/unix HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/STARTUP/XNLOADER.SYS HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/images/xen/vmlinuz HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/images/boot.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/boot/boot.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/current/images/netboot/mini.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:27 +0530] "HEAD /sk/install/images/boot.iso HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/ HTTP/1.1" 200 169 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/.treeinfo HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
1    27.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/Fedora HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/Server HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/Client HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/RedHat HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/CentOS HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/SL HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/directory.yast HTTP/1.1" 404 182 "-" "Python-urllib/2.6"
127.0.0.1 - - [08/Mar/2011:00:26:35 +0530] "HEAD /sk/current/images/MANIFEST HTTP/1.1" 404 182 "-" "Python-urllib/2.6"

How can I remove date from above logs using a sed script [08/Mar/2011:00:26:35 +0530] like this there are many instances.

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

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

发布评论

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

评论(3

白鸥掠海 2024-10-29 20:46:49

您可以使用:

sed -r 's/\[[0-9]{2}\/[A-Z][a-z]{2}\/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2} \+[0-9]+\]//g'

在 Ideone 上查看

或者,如果没有其他出现[..] 在输入中你可以这样做:

sed  's/\[.*\]//g'

You can use:

sed -r 's/\[[0-9]{2}\/[A-Z][a-z]{2}\/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2} \+[0-9]+\]//g'

See it on Ideone

Alternatively if there are no other occurrences of [..] in the input you can just do:

sed  's/\[.*\]//g'
第几種人 2024-10-29 20:46:49

如果你有 Ruby(1.9+)

$ ruby -i.bak -ne 'print $_.gsub(/\[.*?\]/,"")' file

如果你绝对必须使用 sed

$ sed -i.bak 's/\[.[^]]*\]//g' file

If you have Ruby(1.9+)

$ ruby -i.bak -ne 'print $_.gsub(/\[.*?\]/,"")' file

if you absolutely must use sed

$ sed -i.bak 's/\[.[^]]*\]//g' file
傲世九天 2024-10-29 20:46:49

一般模式是:

sed -e 's/pattern/replacement/' filename

With:

-e command

    Append the editing commands specified by the command argument 
    to the list of commands.

在您的情况下,这可能是例如:

sed -e 's/\[.*\]//' yourfilename.log

请注意,只要您没有额外的 ][.*] 就会正常工作一行中的字符。

The general pattern would be:

sed -e 's/pattern/replacement/' filename

With:

-e command

    Append the editing commands specified by the command argument 
    to the list of commands.

In your case, this could be e.g.:

sed -e 's/\[.*\]//' yourfilename.log

Note that [.*] will work correctly as long as you don't have additional ] characters in a line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文