Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split and count number of occurences of word in Bash?

everyone.

I have a file with names in a format :

Name Name    Surname Surname

This is the file with Names

Nikola  KAZIKOVA
Pavel   MILAN GAZDIK
Nikolas Martin  STRUP
Nikola  GAZDIK
Nikola ČERNÁ
Nikola Martina ČERNÁ

I am trying to create a script that prints a number of occurences next to each name. However I can't figure out how to count them.

This is my code, I can load the text into an array but I can't figure out how to count the names.

#!/bin/bash

file=$1

if [[ -z $1 ]]
    then echo "ERROR: FILE NOT FOUND"
    exit
fi


# Read the file in parameter and fill the array named "array"
getArray() {
    array=() # Create array
    while IFS= read -r line # Read a line
    do
        array+=("$line") # Append line to the array
    done < "$1"
}

# Print the file (print each element of the array)
getArray $file
for e in "${array[@]}"
do
    IFS='   ' read -ra arr <<< "$e"
    echo "${arr[0]}" | grep -o "${arr[0]}"

done

This is the result I am trying to achieve

   [4] Nikola  KAZIKOVA
   [1] Pavel   MILAN GAZDIK
   [1] Nikolas Martin  STRUP
   [4] Nikola  GAZDIK
   [4] Nikola ČERNÁ
   [4] Nikola Martina ČERNÁ
like image 638
mitas1c Avatar asked Oct 30 '25 12:10

mitas1c


1 Answers

One option:

while read -r f l ; do echo "[$(grep -w -c $f d.dat)] $f $l" ; done <d.dat

Output:

[4] Nikola KAZIKOVA
[1] Pavel MILAN GAZDIK
[1] Nikolas Martin  STRUP
[4] Nikola GAZDIK
[4] Nikola ČERNÁ
[4] Nikola Martina ČERNÁ

Note, this is not terribly efficient due to the grep inside a loop. If your file is large, then you may want to utilize alternate tools like. awk or python.


awk alternative;

awk 'NR==FNR{A[$1]++}NR>FNR{for(i in A) {if(i==$1){printf "[%s] %s\n", A[i], $0}}}' d.dat d.dat

Output:

[4] Nikola  KAZIKOVA
[1] Pavel   MILAN GAZDIK
[1] Nikolas Martin  STRUP
[4] Nikola  GAZDIK
[4] Nikola ČERNÁ
[4] Nikola Martina ČERNÁ
like image 188
j_b Avatar answered Nov 02 '25 03:11

j_b