首页 > 试题广场 >

第二列是否有重复

[编程题]第二列是否有重复
  • 热度指数:30441 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个nowcoder.txt文件,其中有3列信息,如下:
20201001 python 99
20201002 go 80
20201002 c++ 88
20201003 php 77
20201001 go 88
20201005 shell 89
20201006 java 70
20201008 c 100
20201007 java 88
20201006 go 97
编写一个shell脚本来检查文件第二列是否有重复,且有几个重复,并提取出重复的行的第二列信息(先按次数排序,如果次数相同,按照单词字母顺序排序),输入如下
2 java
3 go
示例1

输入

20201001 python 99
20201002 go 80
20201002 c++ 88
20201003 php 77
20201001 go 88
20201005 shell 89
20201006 java 70
20201008 c 100
20201007 java 88
20201006 go 97

输出

2 java
3 go
awk '{print $2}' nowcoder.txt | sort | uniq -cd | sort -n

发表于 2021-01-12 00:37:53 回复(0)
awk '
    {x[$2]++}END{
    for(i in x){
        if(x[i]>1)
            print x[i],i
    }
 }
'

发表于 2021-08-01 16:13:35 回复(1)
cat  $1 |awk '{print $2}'  |sort  |uniq -c|sort |grep -v 1
发表于 2021-07-09 13:15:39 回复(0)
cat nowcoder.txt |awk '{ \
count[$2]++} \
END{ \
for(i in count){if(count[i] > 1){ \
print sount[i],i} \
} \
}' 
发表于 2021-03-13 03:36:04 回复(1)
awk '{a[$2]++} END{for(i in a) {if(a[i]>=2){print a[i]" "i}}}' nowcoder.txt 

发表于 2021-01-07 17:53:35 回复(0)
cat  $1 |awk '{print $2}'  |sort  |uniq -c|sort |grep -v 1
发表于 2020-11-12 21:24:34 回复(0)
 awk '{print $2}' nowcoder.txt |sort|uniq -c -d| sort -k1n

发表于 2025-03-12 21:26:21 回复(0)
awk '{print $2}' nowcoder.txt | sort | uniq -c |sort -n |tail -2
发表于 2025-01-07 17:10:44 回复(0)
#!/bin/bash
#获取第二列字段并且去除掉空行
awk '{print $2}' nowcoder.txt | sed '/^$/d'  > ok.txt

#按字段排序,获取每个字段的个数,将字段个数>1的字段和个数打印出来
cat ok.txt | sort | uniq -c | sort -n |awk '{if($1 > 1) print $2,$1}'

发表于 2024-12-18 10:14:55 回复(1)
cat nowcoder.txt|sort -k2|awk '{print$2}'|uniq -c|awk '$1 != 1'|sort
发表于 2024-08-06 23:50:12 回复(0)
file="nowcoder.txt"
cat $file | awk '{print $2}' | sort -f | uniq -c | awk '{if($1 > 1){print $1,$2} }' |sort -k 1n -k 2f
发表于 2024-07-03 11:30:54 回复(0)
awk '
{
    data[$2]++;
}
END {
    for(i in data){
        if(data[i] >= 2){
            print data[i] " " i
        }
    }
}
' nowcoder.txt | sort -k1 -k2
发表于 2024-06-14 20:42:22 回复(0)
#!/bin/bash

# Check if file nowcoder.txt exists
if [ ! -f "nowcoder.txt" ]; then
    echo "File nowcoder.txt does not exist."
    exit 1
fi

# Extract the second column assuming that it may not always be neatly spaced.
# We use awk to handle irregular spacing and potential conflation with third column values.
awk '{
    if (NF == 3) {  # If there are exactly three fields, print the second one.
        print $2
    } else if (NF == 2) {  # If there are exactly two fields, it might be missing a space between the second and third column.
        # Extract what looks like a word before a sequence of digits.
        # This regular expression assumes that the word ends when digits start.
        match($2, /^[a-zA-Z]+/)
        if (RSTART > 0) {
            print substr($2, RSTART, RLENGTH)
        }
    }
}' nowcoder.txt | sort | uniq -c | sort -k1,1n -k2,2 | awk '$1 > 1 {print $1 " " $2}'

发表于 2024-05-23 12:08:56 回复(0)
cat nowcoder.txt|awk '{print $2}'|sort|uniq -cd|sort
发表于 2024-04-26 16:31:11 回复(0)
awk '{print $2}' | sort | uniq -c | sort -n | awk '$1!=1{print $0}'
编辑于 2024-02-16 16:45:23 回复(0)
awk '{print $2}' nowcoder.txt |sort |uniq -cd |sort
发表于 2023-11-24 14:43:47 回复(0)
 awk '{print $2}' nowcoder.txt|sort|uniq -c|awk '$1>1{print $1,$2}'|sort
发表于 2023-10-21 18:59:16 回复(0)
awk '{
    if($0 && $2)
        map[$2]++;
}END{
    for(j in map)
        if(map[j] > 1)
            printf("%d %s\n", map[j], j);
}' | sort -nk1 -k2
发表于 2023-09-22 10:51:36 回复(0)
cut -f2 -d' ' | sort | uniq -d -c | sort 


发表于 2023-08-25 16:14:22 回复(0)
awk '{
    mp[$2]+=1;
}
END{
    for (i in mp){
    if(mp[i]>1)
        print mp[i]" "i
    }
 
}' nowcoder.txt | sort -n
发表于 2023-07-05 14:40:57 回复(0)