LInux 排序/uniq apache 日志

发布于 2024-11-04 18:56:33 字数 207 浏览 1 评论 0原文

我有小文件(100)行网络请求(apache std 格式),有来自客户端的多个请求。我只想在我的文件中包含来自唯一 IP 的请求(行)列表,并且是

迄今为止我拥有的 最新条目 /home/$: cat all.txt | awk '{ 打印 $1}' |排序 -u | “{打印整行??}”

上面给了我IP(大约30,这是正确的)现在我还需要该行的其余部分(请求)。

I have small file (100) lines of web request(apache std format) there are multiple request from clients. I want to ONLY have a list of request(lines) in my file that comes from a UNIQUE IP and is the latest entry

I have so far
/home/$: cat all.txt | awk '{ print $1}' | sort -u | "{print the whole line ??}"

The above gives me the IP's(bout 30 which is right) now i need to have the rest of the line(request) as well.

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

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

发布评论

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

评论(3

╄→承喏 2024-11-11 18:56:33

使用关联数组来跟踪您已经找到的 IP:

awk '{  
  if (!found[$1]) {
    print;
    found[$1]=1;
  }
}' all.txt

这将打印每个 IP 的第一行。如果你想要最后一个,那么:

awk '
     { found[$1] = $0 }
     END {
       for (ip in found)
         print found[ip]
     }
' all.txt

Use an associative array to keep track of which IPs you've found already:

awk '{  
  if (!found[$1]) {
    print;
    found[$1]=1;
  }
}' all.txt

This will print the first line for each IP. If you want the last one then:

awk '
     { found[$1] = $0 }
     END {
       for (ip in found)
         print found[ip]
     }
' all.txt
喜你已久 2024-11-11 18:56:33

我讨厌 unique 没有提供与 sort 相同的选项,或者 sort 不能做到它所说的,我认为这应该工作[1],

tac access.log | sort -fb -k1V -u

但可惜,它没有;

因此,我们似乎陷入了做一些愚蠢的事情,

cat all.txt | awk '{ print $1}' | sort -u | while read ip
do
    tac all.txt | grep "^$ip" -h | head -1
done

Which确实效率低下,但是“有效”(尚未测试过:模块拼写错误) [1]

比如根据手册页,

I hate that unique doesn't come with the same options as sort, or that sort cannot do what it says, I reckon this should work[1],

tac access.log | sort -fb -k1V -u

but alas, it doesn't;

Therefore, it seems we're stuck at doing something silly like

cat all.txt | awk '{ print $1}' | sort -u | while read ip
do
    tac all.txt | grep "^$ip" -h | head -1
done

Which is really inefficient, but 'works' (haven't tested it: module typo's then)

[1] according to the man-page

同展鸳鸯锦 2024-11-11 18:56:33

以下应该可行:

tac access.log | sort -f -k1,1 -us

这以相反的顺序获取文件并使用第一个字段进行稳定排序,仅保留唯一的项目。

The following should work:

tac access.log | sort -f -k1,1 -us

This takes the file in reverse order and does a stable sort using the first field, keeping only unique items.

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