UNIX & GNU/Linux - awk - Count number of words in a file and check a name in another

In this awk tutorial we're going to see how to count words in a file and display it.

And in a second time, we will see how to check if a name, inside a file, is present in the first file.

Let's check it.

Explanation

In the BEGIN pattern we specify the separator with the FS builtin variable. In our case, it will be the default one, the space character.

In the dev pattern, we are going to retrieve each word. We use the FNR and the NF builtin variables.

FNR returns the number of the current line.
NF returns the number of words on the line.

Then we add all the words inside the dic[] array.

In the END pattern, we check if the word inside the "name.txt" file is present inside the "students.txt" one.

Code

students.txt

John 5 12 8
Akio 15 16 14
Ortallia 2 8 6
Markus 11 9 12

name.txt

Akio

bp8.awk

#!/bin/awk

# Begin
BEGIN {
    FS=" ";
}

# Dev
{
    i = 0;
    nbWords = 0;
    while (i < FNR) {
        nbWords += NF;
        i++;
    }

    for (word = 1; word <= NF; word++) {
        dic[$word]++;
    }

}

# End
END {
    getline < "name.txt";
    printf "There are " nbWords " words and " FNR " lines.\n" ;
    for (k in dic) {
        if (k == $0)
            printf("The word %s has been found %i time.\n", k, dic[k]);
    }
}

Executing

awk -f bp8.awk students.txt

Output

There are 16 words and 4 lines.
The word Akio has been found 1 time.

Finally

We are able now to count words and check if a word in a file is present also in another.
Well done, you've made it. angel

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.