Count occurrences of a word in string: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(→BASH) |
||
| Line 42: | Line 42: | ||
== BASH == | == BASH == | ||
data preparation | |||
* (1) separate each string by [[Return symbol | return_symbol]] (2) [https://www.computerhope.com/unix/uuniq.htm uniq command] on Cygwin of {{Win}} or {{Linux}} | * (1) separate each string by [[Return symbol | return_symbol]] | ||
* (2) check the [https://www.computerhope.com/unix/uuniq.htm uniq command] is exists on Cygwin of {{Win}} or {{Linux}} | |||
* (3) execute the following command {{kbd | key=<nowiki>sort <file.txt> | uniq -ic | sort -nr</nowiki>}}<ref>[https://unix.stackexchange.com/questions/134446/counting-the-occurrences-of-the-string text processing - Counting the occurrences of the string - Unix & Linux Stack Exchange]</ref> | |||
file: test.txt | file: test.txt | ||
| Line 57: | Line 59: | ||
Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -ic | sort -nr</nowiki>}} | Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -ic | sort -nr</nowiki>}} | ||
<pre> | <pre> | ||
2 #Apple | |||
1 #電影 | 1 #電影 | ||
1 #追劇 | 1 #追劇 | ||
| Line 64: | Line 66: | ||
</pre> | </pre> | ||
Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -c</nowiki>}} | |||
<pre> | |||
1 #Apple | |||
1 #apple | |||
1 #綜藝 | |||
1 #藍芽 | |||
1 #追劇 | |||
1 #電影 | |||
</pre> | |||
== References == | == References == | ||
Revision as of 11:36, 19 November 2019
Counting number of occurrences (or frequency) of a word in string
Excel
- Using the function SUBSTITUTE & LEN functions. demo. Or
- Using the function COUNTIF
MySQL way
SET @paragraph := 'an apple a day keeps the doctor away'; SET @term := 'apple'; SELECT FLOOR((LENGTH(@paragraph) - LENGTH(REPLACE(@paragraph, @term, ''))) / LENGTH(@term)) AS occurrences; /* same with the following query */ SELECT FLOOR((CHAR_LENGTH(@paragraph) - CHAR_LENGTH(REPLACE(@paragraph, @term, ''))) / CHAR_LENGTH(@term)) AS occurrences;
-- Count occurrences of a string: . SET @input = "www.google.com"; SET @separator = "."; SELECT (LENGTH(@input ) - LENGTH(REPLACE(@input , @separator, ""))) / LENGTH(@separator) AS count_of_separator; -- expected result: 2 -- Count occurrences of a string: og SET @input = "www.google.com"; SET @separator = "og"; SELECT (LENGTH(@input ) - LENGTH(REPLACE(@input , @separator, ""))) / LENGTH(@separator) AS count_of_separator; -- expected result: 1
PHP
Using the mb_substr_count (binary safe) or substr_count functions. See details on demo.
$input = 'an apple a day keeps the doctor away'; $term = 'apple'; echo substr_count($input, $term);
BASH
data preparation
- (1) separate each string by return_symbol
- (2) check the uniq command is exists on Cygwin of Win
or Linux
- (3) execute the following command sort <file.txt> | uniq -ic | sort -nr[1]
file: test.txt
#apple #追劇 #電影 #綜藝 #Apple #藍芽
Result of the execution of command: sort test.txt | uniq -ic | sort -nr
2 #Apple 1 #電影 1 #追劇 1 #藍芽 1 #綜藝
Result of the execution of command: sort test.txt | uniq -c
1 #Apple 1 #apple 1 #綜藝 1 #藍芽 1 #追劇 1 #電影
References
- mysql - Count the number of occurences of a string in a VARCHAR field? - Stack Overflow [Last visited: 2016-09-21]