1

Given input file

z
b
a
f
g
a
b
...

I want to output the number of occurrences of each string, for example:

z 1
b 2
a 2
f 1
g 1

How can this be done in a bash script?

6 Answers 6

4

You can sort the input and pass to uniq -c:

$ sort input_file | uniq -c
 2 a
 2 b
 1 f
 1 g
 1 z

If you want the numbers on the right, use awk to switch them:

$ sort input_file | uniq -c | awk '{print $2, $1}'
a 2
b 2
f 1
g 1
z 1

Alternatively, do the whole thing in awk:

$ awk '
{
    ++count[$1]
}
END {
    for (word in count) {
        print word, count[word]
    }
}
' input_file
f 1
g 1
z 1
a 2
b 2
Sign up to request clarification or add additional context in comments.

Comments

1
cat text | sort | uniq -c

should do the job

Comments

1

Try:

awk '{ freq[$1]++; } END{ for( c in freq ) { print c, freq[c] } }' test.txt

Where test.txt would be your input file.

Comments

1

Here's a bash-only version (requires bash version 4), using an associative array.

#! /bin/bash

declare -A count
while read val ; do
    count[$val]=$(( ${count[$val]} + 1 ))
done < your_intput_file # change this as needed

for key in ${!count[@]} ; do
    echo $key ${count[$key]}
done

2 Comments

requires bash version 4 for associative arrays
Simpler: (( count[$val]++ )). Also, you should almost always use -r with read. Always quote variables: for key in "${!count[@]}" and echo "$key ${count[$key]}".
0

This might work for you:

cat -n file | 
sort -k2,2 | 
uniq -cf1 | 
sort -k2,2n | 
sed 's/^ *\([^ ]*\).*\t\(.*\)/\2 \1/'

This output the number of occurrences of each string in the order in which they appear.

Comments

0

You can use sort filename | uniq -c.

Have a look at the Wikipedia page on uniq.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.