Home > Blockchain >  Finding the count of a word in a list of files in linux
Finding the count of a word in a list of files in linux

Time:12-04

I have a list of files(more than 300) with similar names. Example: abc01, abc02, abc03..... How can I find the number of times a specific word occurs in all these files in linux? I have used this:

cat abc* | grep 20211203 | wc -l

Suppose i need to find a list of specific dates in the format yyyymmdd from these files and their counts, what should i include more?For example if the value 20211203 is occuring 4 times and the value 20211202 is occuring 12 times and so on, is there a way to get the list of these value(like the format given below)?

4 20211203

12 20211202

.

.

. and so on

CodePudding user response:

I assume these as txt files and are in same folder.

Then open the folder in VScode and do Ctrl Shift F. Then type the word you want.

CodePudding user response:

Try this:

cat abc* | grep -c word

where word is your word to be searched.

you can also loop this command in a bash file to work it for multiple words:

for element in word1 word2 word3
do
  count=$(cat abc* | grep -c $element)
  echo "$element: $count"
done

you can further pass array of words or files to be searched to make this script more customizable.

  • Related