Count instances of a word in a text file




















Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked today. Active today. Viewed 58 times. Imagine the text is something like: Over time, computer engineers take advantage of each other's work and invent algorithms for new things. Output: Over 1 time 1 computer 1 algotitms If possible I want to ignore if it's upper or lower case, I want to count them both together.

Improve this question. Miguel Rodrigues. Miguel Rodrigues Miguel Rodrigues 13 4 4 bronze badges. Read the text file, 2.

At this point you have the word in your hands and can lowercase it or do other transformations. Implementing a tree map is the hard part. Using it to compute word frequency is the easy part. So, I don't know which part you have completed. Make sure you have a working tree map first. I want to count the words to then "save" them in the treemap, it's just an exercise I want to solve — Miguel Rodrigues.

MiguelRodrigues you should the treemap to compute the word count. I assume you are implementing the tree map using a binary search tree. That's purpose is for quick look up. Learn more. How to find the number of occurrences of a string in file using windows command line? Ask Question. Asked 9 years, 11 months ago.

Active 2 years, 2 months ago. Viewed 70k times. Improve this question. Luke Girvin Patryk Patryk Add a comment. Active Oldest Votes. Improve this answer. Adam S Adam S 2, 1 1 gold badge 34 34 silver badges 41 41 bronze badges.

Patryk, my mistake, I misread that all emails were on each on their own line. Will revise. Very simple solution: grep -o " " mail. Remember a dot at end of line! Here is little bit more understandable way: grep -o " " mail. Second grep counts lines or lines with. Where tr replaces spaces with newlines, grep filters all resulting lines matching WORD and wc counts the remaining ones.

If it is not guaranteed that there are spaces between the words, you have to use some other character as delimiter to replace. For example alternative tr parts are. Of course, you can also use tr to replace multiple characters at once think different kinds of whitespace and punctuation. If it works on your platform, it's an elegant and fairly intuitive solution; but the GNU folks are still thinking. From: torvalds klaava. This has been brewing since april, and is starting to get ready.

Depending on whether you'd like to match the word in the keys or in the values of the JSON data, you are likely to want to extract only keys or only values from the data. Otherwise you may count some words too many times if they occur as both keys and values. This recursively tests whether the current thing is an object, and if it is, it extracts the keys. The output will be a list of keys, one per line.

I have json with something like this: "number":"OK","number":OK" repeated multiple times in one line. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. How do I count the number of occurrences of a word in a text file with the command line?

Ask Question. If you like what you are reading, please consider buying us a coffee or 2 as a token of appreciation. We are thankful for your never ending support. While grep -c works on a line, this puts every word or number on its own line and sorts them. Then uniq -c deduplicates them as well as printing the number of occurrences.

The final sort is optional, used to list the words by frequency of appearance instead of alphanumerically. Note the apostrophe in the first set given to tr such that possessives and contractions remain whole words but parenthesis, quotation marks, and other punctuation are stripped off. Also note that this breaks on longer, comma-separated numbers, turning each group into a probably meaningless lone 1-, 2-, or 3-digit number.

Have a question or suggestion?



0コメント

  • 1000 / 1000