无法使用 uniq -c 打印所有行的计数

Unable to use uniq -c to print counts for all lines

我想用uniq统计每一行出现的次数,不包括行首的数值(例子:0000:01:00.0)

每次出现不止一次。可能会出现每次只出现一次的情况。

示例输入

[root@cpod-compute-1 ~]# for i in $(awk -F= '/PCI_SLOT_NAME/{print }' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i; done
0000:01:00.0 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Cisco Systems Inc" "Device 00d5"
0000:01:00.1 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Cisco Systems Inc" "Device 00d5"
0000:0a:00.1 "Ethernet controller" "Intel Corporation" "Ethernet Controller X710 for 10GbE SFP+" -r01 "Cisco Systems Inc" "Ethernet Converged NIC X710-DA"
0000:0a:00.2 "Ethernet controller" "Intel Corporation" "Ethernet Controller X710 for 10GbE SFP+" -r01 "Cisco Systems Inc" "Ethernet Converged NIC X710-DA"
0000:0a:00.3 "Ethernet controller" "Intel Corporation" "Ethernet Controller X710 for 10GbE SFP+" -r01 "Cisco Systems Inc" "Ethernet Converged NIC X710-DA"

预期输出

2  "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Cisco Systems Inc" "Device 00d5"
3  "Ethernet controller" "Intel Corporation" "Ethernet Controller X710 for 10GbE SFP+" -r01 "Cisco Systems Inc" "Ethernet Converged NIC X710-DA"

非工作案例示例

[root@cpod-compute-1 ~]#  for i in $(awk -F= '/PCI_SLOT_NAME/{print }' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i | awk '{="";print [=12=]}' | sort | uniq -c; done
      1  "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Cisco Systems Inc" "Device 00d5"
      1  "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Cisco Systems Inc" "Device 00d5" 
      1  "Ethernet controller" "Intel Corporation" "Ethernet Controller X710 for 10GbE SFP+" -r01 "Cisco Systems Inc" "Ethernet Converged NIC X710-DA"
      1  "Ethernet controller" "Intel Corporation" "Ethernet Controller X710 for 10GbE SFP+" -r01 "Cisco Systems Inc" "Ethernet Converged NIC X710-DA"
      1  "Ethernet controller" "Intel Corporation" "Ethernet Controller X710 for 10GbE SFP+" -r01 "Cisco Systems Inc" "Ethernet Converged NIC X710-DA"

收集的输出 - 我在收集的输出中没有看到任何意外字符。

[root@cpod-compute-1 ~]#  for i in $(awk -F= '/PCI_SLOT_NAME/{print }' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i | awk '{="";print [=13=]}' | od -c; done
0000000       "   E   t   h   e   r   n   e   t       c   o   n   t   r
0000020   o   l   l   e   r   "       "   I   n   t   e   l       C   o
0000040   r   p   o   r   a   t   i   o   n   "       "   I   3   5   0
0000060       G   i   g   a   b   i   t       N   e   t   w   o   r   k
0000100       C   o   n   n   e   c   t   i   o   n   "       -   r   0
0000120   1       "   C   i   s   c   o       S   y   s   t   e   m   s
0000140       I   n   c   "       "   D   e   v   i   c   e       0   0
0000160   d   5   "  \n
0000164
0000000       "   E   t   h   e   r   n   e   t       c   o   n   t   r
0000020   o   l   l   e   r   "       "   I   n   t   e   l       C   o
0000040   r   p   o   r   a   t   i   o   n   "       "   I   3   5   0
0000060       G   i   g   a   b   i   t       N   e   t   w   o   r   k
0000100       C   o   n   n   e   c   t   i   o   n   "       -   r   0
0000120   1       "   C   i   s   c   o       S   y   s   t   e   m   s
0000140       I   n   c   "       "   D   e   v   i   c   e       0   0
0000160   d   5   "  \n
0000164

[root@cpod-compute-1 ~]# for i in $(awk -F= '/PCI_SLOT_NAME/{print }' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i | awk '{="";print [=13=]}' | sed -n l; done
 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Conn\
ection" -r01 "Cisco Systems Inc" "Device 00d5"$
 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Conn\
ection" -r01 "Cisco Systems Inc" "Device 00d5"$

我无法理解看起来重复的相邻行有什么区别。

如果需要任何进一步的输出,请告诉我。

谢谢。

OP 使用的代码是:

for i in $(awk -F= '/PCI_SLOT_NAME/{print }' /sys/class/net/*/device/uevent); do 
   lspci -Dm | grep $i | awk '{="";print [=10=]}' | sort | uniq -c;
done

虽然它应该是:

for i in $(awk -F= '/PCI_SLOT_NAME/{print }' /sys/class/net/*/device/uevent); do 
   lspci -Dm | grep $i | awk '{="";print [=11=]}'
done | sort | uniq -c;

这两种情况的区别在于,在第一种情况下,您对 单个 命令的输出进行排序:

lspci -Dm | grep $i | awk '{="";print [=12=]}'

在第二种情况下,您对整个 for 循环的输出进行排序。

更新版本如下:

awk -F= '(FNR==1){ OFS=FS=(f==0 ? "=" : " "); [=13=]=[=13=] }
         (f==0) && /PCI_SLOT_NAME/{a[];next}
         ( in a) {=""; [=13=]=[=13=]; =; print}
        ' /sys/class/net/*/device/uevent f=1 <(lspci -Dm) | sort | uniq -c

我们现在仍然可以在 awk 中合并计数:

awk -F= '(FNR==1){ OFS=FS=(f==0 ? "=" : " "); [=14=]=[=14=] }
         (f==0) && /PCI_SLOT_NAME/{a[];next}
         ( in a) {=""; [=14=]=[=14=]; =; b[[=14=]]++}
         END{for (i in b) print b[i],i}
        ' /sys/class/net/*/device/uevent f=1 <(lspci -Dm)