简体   繁体   English

Bash 计算使用 uniq 数组

[英]Bash couting using uniq an array

arrays are driving my crazy on Linux. arrays 让我对 Linux 发疯。 I two arrrays called x, y.我有两个数组称为 x, y。 Those arrays contain one IP that repeats a crazy amount of times.那些 arrays 包含一个重复疯狂次数的 IP。

x=(182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59 182.100.67.59)

y=(218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 
218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 
218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 
218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22 218.52.41.22)


How can I count variables x, and y.如何计算变量 x 和 y。 Using something like uniq.使用类似 uniq 的东西。

(IFS=""; sort <<< "$x") | uniq -c

But it does not work但它不起作用

EDIT 1:编辑1:

Desired Output:所需的 Output:

6 182.100.67.59

24 218.52.41.22

EDIT 2:编辑2:

I have implemented this simple for:我已经实现了这个简单的:

for i in $x; do
            
            echo $i | uniq -c
            
            done

It prints out它打印出来


      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59
      1 182.100.67.59

But the desired output would be:但所需的 output 将是:

15 182.100.67.59

First print the array values on lines.首先在行上打印数组值。
Then sort.然后排序。
Then uniq.然后是独特的。

printf "%s\n" "${x[@]}" "${y[@]}" | sort | uniq -c

Your code is just printing first value.您的代码只是打印第一个值。 To read the whole array just add $x[*]要读取整个数组,只需添加$x[*]

(IFS=""; sort <<< "$x[*]") | uniq -c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM