将文件的最后 n 行添加到 tar/zip
我需要定期发送一组可能会变得非常大的日志文件,因此我只想发送每个文件的最后 n 行。
例如:
/usr/local/data_store1/file.txt (500 lines)
/usr/local/data_store2/file.txt (800 lines)
给定一个包含名为 files.txt
的所需文件列表的文件,我想使用每个文件的最后 100 行创建一个存档(tar 或 zip)。
我可以通过使用尾部文件创建一个单独的目录结构来做到这一点,但这似乎是浪费资源,因为可能有一些管道魔法可以完成它。 还必须保留完整的目录结构,因为文件在不同的目录中可以具有相同的名称。
如果可能的话,我希望解决方案是 shell 脚本,但是 perl (不添加模块)也是可以接受的(这是针对没有安装 ruby/python/etc.. 的 Solaris 机器。)
I need to regularly send a collection of log files that can grow quite large, so I would like to only send the last n lines of the each of the files.
for example:
/usr/local/data_store1/file.txt (500 lines)
/usr/local/data_store2/file.txt (800 lines)
Given a file with a list of needed files named files.txt
, I would like to create an archive (tar or zip) with the last 100 lines of each of those files.
I can do this by creating a separate directory structure with the tail-ed files, but that seems like a waste of resources when there's probably some piping magic that can happen to accomplish it. Full directory structure also must be preserved since files can have the same names in different directories.
I would like the solution to be a shell script if possible, but perl (without added modules) is also acceptable (this is for Solaris machines that don't have ruby/python/etc.. installed on them.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试
其中
a.zip
是 zip 文件,10 是n
或tar.gz
You could try
where
a.zip
is the zip file and 10 isn
orfor tar.gz
您专注于特定的实施,而不是着眼于更大的前景。
如果最终目标是在目标计算机上拥有文件的精确副本,同时最大限度地减少传输的数据量,那么您应该使用 rsync,它会自动仅发送文件中已更改的部分,并且还可以在同时自动压缩文件。发送时并在接收时解压缩。
与标准 sshd 相比,运行 rsync 不需要在目标计算机上安装更多守护进程,并且要设置无需密码的自动传输,您只需要使用公钥身份验证。
You are focusing in an specific implementation instead of looking at the bigger picture.
If the final goal is to have an exact copy of the files on the target machine while minimizing the amount of data transfered, what you should use is rsync, which automatically sends only the parts of the files that have changed and also can automatically compress while sending and decompress while receiving.
Running rsync doesn't need any more daemons on the target machine that the standard sshd one, and to setup automatic transfers without passwords you just need to use public key authentication.
没有管道魔法可以做到这一点,您必须创建所需的文件夹结构并将其压缩。
There is no piping magic for that, you will have to create the folder structure you want and zip that.
使用logrotate。
查看
/etc/logrotate.d
内部的示例。Use logrotate.
Have a look inside
/etc/logrotate.d
for examples.为什么不将日志文件放入 SCM 中?
您的接收者在他的计算机上创建一个存储库,通过检出文件来检索文件。
您只需提交文件即可发送它们。 仅传输差异。
Why not put your log files in SCM?
Your receiver creates a repository on his machine from where he retrieves the files by checking them out.
You send the files just by commiting them. Only the diff will be transmitted.