Din*_*ing 110
您可以尝试使用uniq man uniq并执行以下操作
sort file | uniq -u | wc -l
Run Code Online (Sandbox Code Playgroud)
这是我如何解决问题:
... | awk '{n[$0]++} END {for (line in n) if (n[line]==1) num++; print num}'
Run Code Online (Sandbox Code Playgroud)
但那是非常不透明的.这是一个(稍微)更清晰的方式来看它(需要bash版本4)
... | {
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
}
Run Code Online (Sandbox Code Playgroud)