【问题标题】:Combine sed truncate x lines into a find command将 sed truncate x 行组合成一个查找命令
【发布时间】:2019-02-11 16:06:37
【问题描述】:

我们在多台服务器的同一位置有一个大型日志文件,我想创建一个 cron 作业来将文件截断到最后 100k 行。

以下命令有效:

sed -i 1,$(($(wc -l < /root/server123.example.com.log) -100000))d /root/server123.example.com.log

但是每个服务器上的主机名是不同的(server1、server2、server3 等),我想要一个可以粘贴到每个 cron 文件中的命令。在我的测试过程中,我不确定如何在上述命令中实现通配符。

我认为最好的方法可能是将它与 find 命令结合使用,但我不知道如何做到这一点..

find /root/server*.example.com.log -type f -exec sed <NOT SURE..> \;

任何帮助将不胜感激。

【问题讨论】:

  • 为此使用logrotate。除此之外,这个问题与 SO 无关,因为它与编程无关。 superuser.com 会更合适。但请始终确保您不要问以前没有回答过的问题。

标签: sed find


【解决方案1】:

在我的测试过程中,我不确定如何在上述命令中实现通配符。

如果每台服务器上只有一个日志文件,您可以简单地插入通配符:

sed -i 1,$(($(wc -l < /root/server*.example.com.log) -100000))d /root/server*.example.com.log

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    相关资源
    最近更新 更多