bash 非重复行数 - 唯一计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16327566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:38:53  来源:igfitidea点击:

Number of non repeating lines - unique count

bashshelllineunique

提问by john blackwood

Here is my problem: Any number of lines of text is given from standard input. Output: number of non repeating lines

这是我的问题:从标准输入给出任意数量的文本行。输出:非重复行数

INPUT:

输入:

She is wearing black shoes.
My name is Johny.
I hate mondays.
My name is Johny.
I don't understand you.
She is wearing black shoes.

OUTPUT:

输出:

2

回答by Ding

You could try using uniq man uniqand do the following

您可以尝试使用 uniqman uniq并执行以下操作

sort file | uniq -u | wc -l

回答by glenn Hymanman

Here's how I'd solve the problem:

这是我解决问题的方法:

... | awk '{n[
... | {
    declare -A count    # count is an associative array

    # iterate over each line of the input
    # accumulate the number of times we've seen this line
    #
    # the construct "IFS= read -r line" ensures we capture the line exactly

    while IFS= read -r line; do
        (( count["$line"]++ ))
    done

    # now add up the number of lines who's count is only 1        
    num=0
    for c in "${count[@]}"; do
        if (( $c == 1 )); then
            (( num++ ))
        fi
    done

    echo $num
}
]++} END {for (line in n) if (n[line]==1) num++; print num}'

But that's pretty opaque. Here's a (slightly) more legible way to look at it (requires bash version 4)

但这很不透明。这是一种(稍微)更清晰的查看方式(需要 bash 版本 4)

##代码##