Difference between revisions of "Count occurrences of a word in string"

From LemonWiki共筆
Jump to navigation Jump to search
 
Line 52: Line 52:
 
* (1) separate each string by [[Return symbol | return_symbol]] <ref>[https://www.unix.com/shell-programming-and-scripting/83076-replacing-commas-newlines-using-sed.html replacing comma's with newlines using sed]</ref>
 
* (1) separate each string by [[Return symbol | return_symbol]] <ref>[https://www.unix.com/shell-programming-and-scripting/83076-replacing-commas-newlines-using-sed.html replacing comma's with newlines using sed]</ref>
 
* (2) check the [https://www.computerhope.com/unix/uuniq.htm uniq command] is exists on Cygwin of {{Win}} or {{Linux}}
 
* (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><ref>[https://unix.stackexchange.com/questions/170043/sort-and-count-number-of-occurrence-of-lines Sort and count number of occurrence of lines - Unix & Linux Stack Exchange]</ref>
+
* (3) (optional) input the command {{kbd | key=<nowiki>export LC_ALL='C'</nowiki>}} on Cygwin of {{Win}} if met the error message "Invalid or incomplete multibyte or wide character" after input the following {{kbd | key=<nowiki>sort</nowiki>}} command
* (4) Remove the leading whitespace in the file: Using the [[Text editor with support for regular expression | text editor]] with support for [[Regular expression|regular expression]] and replace {{kbd | key=<nowiki>^\s+(\d+)\s+</nowiki>}} with {{kbd | key=<nowiki>\1\t</nowiki>}}
+
* (4) 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><ref>[https://unix.stackexchange.com/questions/170043/sort-and-count-number-of-occurrence-of-lines Sort and count number of occurrence of lines - Unix & Linux Stack Exchange]</ref>
 +
* (5) Remove the leading whitespace in the file: Using the [[Text editor with support for regular expression | text editor]] with support for [[Regular expression|regular expression]] and replace {{kbd | key=<nowiki>^\s+(\d+)\s+</nowiki>}} with {{kbd | key=<nowiki>\1\t</nowiki>}}
  
 
file: test.txt
 
file: test.txt

Latest revision as of 19:05, 22 September 2020

Counting number of occurrences (or frequency) of a word in string

Excel[edit]

  1. Using the function SUBSTITUTE & LEN functions. demo. Or
  2. Using the function COUNTIF

MySQL way[edit]

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;

online example

-- 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[edit]

Using the mb_substr_count (binary safe) or substr_count functions. See details on demo.

<?php

$input = 'an apple a day keeps the doctor away';
$term = 'apple';

echo substr_count($input, $term) . PHP_EOL;

$input = '一天一蘋果,醫生遠離我';
$term = '蘋果';

echo mb_substr_count($input, $term, 'UTF-8') . PHP_EOL;

BASH[edit]

data preparation

  • (1) separate each string by return_symbol [1]
  • (2) check the uniq command is exists on Cygwin of Win Os windows.png or Linux Os linux.png
  • (3) (optional) input the command export LC_ALL='C' on Cygwin of Win Os windows.png if met the error message "Invalid or incomplete multibyte or wide character" after input the following sort command
  • (4) execute the following command sort <file.txt> | uniq -ic | sort -nr[2][3]
  • (5) Remove the leading whitespace in the file: Using the text editor with support for regular expression and replace ^\s+(\d+)\s+ with \1\t

file: test.txt

#apple
#追劇
#電影
#綜藝
#Apple
#藍芽

Result of the execution of command: sort test.txt | uniq -ic | sort -nr Icon exclaim.gif insensitive

   2 #Apple
   1 #電影
   1 #追劇
   1 #藍芽
   1 #綜藝

Result of the execution of command: sort test.txt | uniq -c Icon exclaim.gif sensitive

   1 #Apple
   1 #apple
   1 #綜藝
   1 #藍芽
   1 #追劇
   1 #電影

verification of count occurrence

cat test.txt | grep -i "#apple$" | wc -l

# or
cat test.txt | grep -iw "#apple" | wc -l

Options[4]

  • -i means Ignore uppercase vs. lowercase.
  • -w means --word-regexp

References[edit]

  • replacing comma's with newlines using sed
  • text processing - Counting the occurrences of the string - Unix & Linux Stack Exchange
  • Sort and count number of occurrence of lines - Unix & Linux Stack Exchange
  • Grep - Wikibooks, open books for an open world