列中唯一值的数量

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

假设一列中有很多行,如下所示:

/aba72?mprag=ABCDD&radgroup=$%VDFVFid%7D&bobuecontext=$%7678XEL_CONTEXT%7D&do_id=$%7BMI%7D

我想知道其中有多少 do_id 是唯一的。可以在 SQl 或 bash 中完成

不知道该怎么做

sql bash unique
1个回答
0
投票

您可以使用纯 Bash 功能来做到这一点:

  • 内部正则表达式匹配
    [[ ]]
  • 关联数组 (
    declare -A
    )
declare -A SEEN
while read -r LINE; do
    if [[ $LINE =~ (^|&)do_id=([^\&]+) ]]; then
        VAL=${BASH_REMATCH[2]}
        (( ++SEEN[$VAL] ))
    fi
done < lines.txt

for SEEN_VAL in "${!SEEN[@]}"; do
    # Values with counts
    printf -- '%s (count: %d)\n' "$SEEN_VAL" \
        "${SEEN[$SEEN_VAL]}"
    # Just the bare values
    #printf -- '%s\n' "$SEEN_VAL"
done

打印带有 do_id 值的行:

$%7BMQ%7D (count: 1)
$%7BMI%7D (count: 2)

警告:

do_id
每行不能出现多次。

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