我有一个excel文件,第10列有多个值,如下所示,
Designation Header
-----------------
Student
Teacher
Teacher
Organization Director
Organization Director
Organization Head
Student
Organization Head
Organization Director
Teacher
我试图从这一列中获得唯一值,并在关联数组中对应每个唯一值的行数。
下面是我用来获取唯一值和行数的命令,
awk -F',' 'NR>1{print $10}' Sorted_File.csv | sort | uniq -c
输出如下:
2 Student
3 Teacher
3 Organization Director
2 Organization Head
我如何保存这个输出在一个关联数组即键值对?
另一个没有awk的bash版本:
declare -A counts
while IFS=, read -r -a ary; do
(( counts[${ary[9]}]++ ))
done < <(tail -n +3 Sorted_File.csv)
declare -p counts
在bash,
中应该是这样的:
#!/usr/bin/env bash
declare -A counts
while read -r cnt what; do
counts[$what]=$cnt
done < <(awk -F',' 'NR>1{print $10}' Sorted_File.csv | sort | uniq -c)
declare -p counts
基本上,在循环中读取原始管道的输出,将每对值添加到关联数组中。