How to count the number of appearances of a word in each line

Shaharg

I have a text file and I would like to count for each line the number of appearances of a given word for example if the word is "text" and the file is

abc text fff text text jjj
fff fff text ddd
eee rrr ttt yyy

I expect the output

3
1
0

How can I achieve this with bash?

PSkocik
while read line; do echo "$line" |tr ' ' '\n' |grep text -c ; done < file 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I count the appearance # of a word (not total appearances)?

Count number of appearances of different values

How do I count the number of appearances within vector of columns?

How to count the number of a specific character in each line?

how to count the number of words in each line in Java?

How to count the number of characters on each line in vim?

How to count the number of characters on each line with group by

How to count the number of occurrences of each word in a sentence for each sentence score?

How to get number of line(not ammount) for each word where this word occured

Count the number of occurrences of each word

SQL - How to aggregate and count appearances

How to efficiently count the number of occurrences of each word in Python

sql: count number of appearances in other table

Count number of appearances in JSON data for nested element

Counting number of appearances of term in each value of a dictionary

How to add second word as a number of each line in a file?

Regex for minimum number of appearances of capturing group on a line

Find word in line and count number of line

How to count number of tabs in each line using shell script?

How to count the appearances of certain string/words on each row in a column in a dataframe using python

How to count the number of appearances for all elements in a list of string by date in pandas dataframe

Count the number occurrences of each word in a text - Python

Count the number of rows that each word appears in

Count number of characters of each word in a cell in excel

Count number of word, line, character in a file

How to store elements and number of appearances in Java

Count number of higher lines for each line pandas

SQL Find the number of orders in each line count

how to count number of "," in each col