5

I need to print the count of a matching string at the end of each line.

An example for matching foo:

foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar

Result :

foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2

I have checked this link(How to count the number of a specific character in each line?) but no luck.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • If foobar existed in a line should that be counted as matching foo or not? What if the string to match was fo. and fox existed in the line - should that count? You asked for a count of strings - you didn't say if it should be full or partial words but in any case at least some of the answers you got are counting partially matching regexps. – Ed Morton Sep 22 '21 at 22:18

4 Answers4

11

We can use awk with gsub to get the count of occurrence.

 awk '{print $0","gsub(/foo/,"")}' file

Output:

foo,bar,foo,foo,3
bar,foo,bar,bar,1
foo,foo,bar,bar,2
Siva
  • 9,077
1

Perhaps with a mixture of bash and grep

$ while read -r line; do 
    echo -n "$line -> " 
    grep -o foo <<<"$line" | wc -l 
  done < /path/to/my-input-file

foo,bar,foo,foo -> 3
bar,foo,bar,bar -> 1
foo,foo,bar,bar -> 2
shalomb
  • 197
0

In case anyone also wanted something in Python. Specify the filename and the magic word to count over.

#!/usr/bin/python3
# magic_word_count.py
# Takes a filename and magic word and prints the number of times the word
# appears on each line of the file.
#
# ./magic_word_count.py myfile.txt foo
#
import sys 
filename = sys.argv[1]
magic_word = sys.argv[2]

with open(filename, 'r') as f:
    for line in f.readlines():
        words = line.strip().split(',')
        print(len([word for word in words if word == magic_word]))

Usage:

$ cat myfile.txt 
foo,bar,foo,foo
bar,foo,bar,bar
foo,foo,bar,bar

$ ./magic_word_count.py myfile.txt foo
3
1
2
user1717828
  • 3,542
0

Using awk and assuming that the input is "simple" CSV (no embedded commas or newlines):

awk -v string="foo" -F, '
    BEGIN { OFS = FS }
    {
        sum = 0
        for (i = 1; i <= NF; ++i) sum += $i == string
        $(NF+1) = sum
    }; 1' file

This takes the string that we want to count occurrences of and then iterates through the fields of each record, counting exact matches. The accumulated sum of matching fields is then added as a new field at the end of the record, and the record is printed.

Kusalananda
  • 333,661