0
A
답변
0
는 AWK를 사용하고자한다면, 당신이 뭔가를 할 수 있습니다 :
$ awk -F] -vRS="," '!(NR%2){++a[$1]}END{for(i in a)printf "%s: %s\n",i,a[i]}' <<<"[[some_str,another_str],[some_str,the_str],[some_str,the_str],[some_str,whatever_str]]"
whatever_str: 1
another_str: 1
the_str: 2
]
에 필드 분리 및 ,
에 레코드 분리를 설정합니다. 모든 두 번째 레코드의 발생을 계산하십시오. 모든 레코드가 처리 된 후 결과를 인쇄하십시오.
4
# read strings into an array, excluding [, ] and , characters
IFS='[],' read -r -a strings <<<'[[some_str,another_str],[some_str,the_str],[some_str,the_str],[some_str,whatever_str]]'
# store counts in an associative array
declare -A counts=()
for string in "${strings[@]}"; do
[[ $string ]] || continue
(('counts[$string]' += 1))
done
# iterate over that associative array and print counters
for string in "${!counts[@]}"; do
echo "$string: ${counts[$string]}"
done
연관 배열에 bash 4 이상이 필요합니다. 또한 일부 bash 4+ 버전에서 수정 된 IFS/read/heredoc/herestring 버그가 있습니다. 여기에는 관련성이 있거나 그렇지 않을 수도 있습니다 (불행히도 세부 정보는 잊어 버렸습니다). –
편집 된 현재 버전 (감사합니다, @gniourf_gnioruf)이 4.0.35에서 작동하므로 최근 수정 된 사항에 의존한다고 생각하지 않습니다. –
내가 생각하고있는 문제가 'IFS'를 리디렉션으로 유출하고 '4.3'에 고정 된 '4.2'버그였습니다. 네, 괜찮습니다. (기록 상으로는 그 사실을 의문시하는 의도가 전혀 없었다.) –