gpt4 book ai didi

linux - 将日志文件解析为多个唯一的日志文件

转载 作者:太空宇宙 更新时间:2023-11-04 05:27:15 25 4
gpt4 key购买 nike

我有一个集中式日志文件,我正在尝试将其解析为多个文件,以使其更易于管理。

该文件包含如下所示的行

2015-04-02 16:03:13 -0500       192.168.3.3: shell login for 'rancid' from 192.168.50.10 on tty1 succeeded
2015-04-02 16:03:20 -0500 192.168.3.8: shell login for 'rancid' from 192.168.50.10 on tty1 succeeded
2015-04-02 16:03:24 -0500 192.168.4.11: shell login for 'rancid' from 192.168.50.10 on tty1 succeeded
2015-04-02 16:03:33 -0500 192.168.4.7: shell login for 'rancid' from 192.168.50.10 on tty1 succeeded
2015-04-02 16:03:34 -0500 192.168.4.8: shell login for 'rancid' from 192.168.50.10 on tty1 succeeded
2015-04-02 16:03:46 -0500 192.168.5.10: shell login for 'rancid' from 192.168.50.10 on tty1 succeeded
2015-04-02 16:03:50 -0500 192.168.5.11: shell login for 'rancid' from 192.168.50.10 on tty1 succeeded

我想分割日志文件,以便与第一个 IP 地址相关的所有行都在同一个日志文件中,以便 192.168.3.8 有它自己的文件,192.168.4.11 有它自己的文件等

最佳答案

试试这个(但要注意你会得到一些文件ip_xxx.xxx.xxx.xxx.log:-)

LOG=logfile_to_be_splitted
awk '{print $4}' ${LOG} | sort -u | while read ip; do
lfile=$(echo "$ip" | sed 's/\(.*\):/ip_\1.log/');
grep "$ip" "$LOG" >$lfile;
done

awk 命令获取包含 IP 地址的列

sort -u 使它们唯一(如果 IP 地址不在连续行中)

sed 第 4 列的 IP 地址,去掉结尾的“:”

while 循环中,IP 会从日志文件中 grep 到相应的文件中

关于linux - 将日志文件解析为多个唯一的日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29422840/

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