无法使用uniq -c来计算重复的行数

问题描述 投票:0回答:1

我无法使用uniq来计算重复行的数量。

输出以执行uniq on

[root@cpod-compute-1 ~]# for i in $(awk -F= '/PCI_SLOT_NAME/{print $2}' /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:09:00.0 "Ethernet controller" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "Cisco Systems Inc" "VIC 1227 PCIe Ethernet NIC"
0000:0a:00.0 "Ethernet controller" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "Cisco Systems Inc" "VIC 1227 PCIe Ethernet NIC"

预期输出

2  "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Cisco Systems Inc" "Device 00d5"
2  "Ethernet controller" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "Cisco Systems Inc" "VIC 1227 PCIe Ethernet NIC"

无效案例的示例

[root@cpod-compute-1 ~]#  for i in $(awk -F= '/PCI_SLOT_NAME/{print $2}' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i | awk '{$1="";print $0}' | 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" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "Cisco Systems Inc" "VIC 1227 PCIe Ethernet NIC"
      1  "Ethernet controller" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "Cisco Systems Inc" "VIC 1227 PCIe Ethernet NIC"

[root@cpod-compute-1 ~]#  for i in $(awk -F= '/PCI_SLOT_NAME/{print $2}' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i | awk '{$1="";count[$0]++} END{for(c in count) print count[c], 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" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "Cisco Systems Inc" "VIC 1227 PCIe Ethernet NIC"
1  "Ethernet controller" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "Cisco Systems Inc" "VIC 1227 PCIe Ethernet NIC"

[已收集的输出-在收集到的输出中看不到任何意外的字符。

[root@cpod-compute-1 ~]#  for i in $(awk -F= '/PCI_SLOT_NAME/{print $2}' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i | awk '{$1="";print $0}' | 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
0000000       "   E   t   h   e   r   n   e   t       c   o   n   t   r
0000020   o   l   l   e   r   "       "   C   i   s   c   o       S   y
0000040   s   t   e   m   s       I   n   c   "       "   V   I   C
0000060   E   t   h   e   r   n   e   t       N   I   C   "       -   r
0000100   a   2       "   C   i   s   c   o       S   y   s   t   e   m
0000120   s       I   n   c   "       "   V   I   C       1   2   2   7
0000140       P   C   I   e       E   t   h   e   r   n   e   t       N
0000160   I   C   "  \n
0000164
0000000       "   E   t   h   e   r   n   e   t       c   o   n   t   r
0000020   o   l   l   e   r   "       "   C   i   s   c   o       S   y
0000040   s   t   e   m   s       I   n   c   "       "   V   I   C
0000060   E   t   h   e   r   n   e   t       N   I   C   "       -   r
0000100   a   2       "   C   i   s   c   o       S   y   s   t   e   m
0000120   s       I   n   c   "       "   V   I   C       1   2   2   7
0000140       P   C   I   e       E   t   h   e   r   n   e   t       N
0000160   I   C   "  \n


[root@cpod-compute-1 ~]# for i in $(awk -F= '/PCI_SLOT_NAME/{print $2}' /sys/class/net/*/device/uevent); do lspci -Dm | grep $i | awk '{$1="";print $0}' | 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"$
 "Ethernet controller" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "C\
isco Systems Inc" "VIC 1227 PCIe Ethernet NIC"$
 "Ethernet controller" "Cisco Systems Inc" "VIC Ethernet NIC" -ra2 "C\
isco Systems Inc" "VIC 1227 PCIe Ethernet NIC"$

我无法理解似乎重复的相邻行之间的区别。

请让我知道是否需要其他输出。

谢谢。

awk uniq
1个回答
0
投票

EDIT1:由于在这种情况下OP更改了问题以获取重复的行值,因此请尝试以下操作。

for i in $(awk -F= '/PCI_SLOT_NAME/{print $2}' /sys/class/net/*/device/uevent)
do
   lspci -Dm | grep $i |\
   awk '{$1="";sub(/^ +/,"");count[$0]++} END{for(c in count){if(count[c]>1){print count[c], c}}'
done


您能不能尝试以下操作,尚未经过测试(以获取唯一值)。由于您开始专注于行中即将出现的空间,因此我尝试解决该问题。现在,为什么要空出输出,因为您要取消$1。因此,我在这里要做的是在处理数组计数逻辑之前用NULL替换初始空间。

for i in $(awk -F= '/PCI_SLOT_NAME/{print $2}' /sys/class/net/*/device/uevent)
do
   lspci -Dm | grep $i |\
   awk '{$1="";sub(/^ +/,"");count[$0]++} END{for(c in count){if(count[c]==1){print count[c], c}}'
done

OP正在做的是,获取所有元素的数量;为了获得唯一元素计数,我们应该将所有元素中的if(count[c]==1放在代码的END块中。同样,我假设您要使数组的任何特定字段的索引都为当前行($ 0),然后将count[$0]++更改为其中的特定字段号。

© www.soinside.com 2019 - 2024. All rights reserved.