Editing
Count occurrences of a word in string
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== BASH == data preparation * (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}} * (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) 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>}} === Input Format A: One term per line === {{exclaim}} Each line contains only one term/keyword file: test.txt <pre> #apple #追劇 #電影 #綜藝 #Apple #藍芽 </pre> ==== Output format I: count followed by keyword ==== {{exclaim}} The term each line in the input file was allowed contains whitespaces. Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -ic | sort -nr</nowiki>}} {{exclaim}} case insensitive <pre> 2 #Apple 1 #電影 1 #追劇 1 #藍芽 1 #綜藝 </pre> Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -c</nowiki>}} {{exclaim}} case sensitive <pre> 1 #Apple 1 #apple 1 #綜藝 1 #藍芽 1 #追劇 1 #電影 </pre> ==== Output format II: keyword followed by count ==== {{exclaim}} The term each line in the input file should '''not''' contains whitespaces. Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -ic | sort -nr | awk ' { t = $1; $1 = $2; $2 = t; print; } '</nowiki>}} <ref>[https://stackoverflow.com/questions/11967776/swap-two-columns-awk-sed-python-perl Swap two columns - awk, sed, python, perl - Stack Overflow]</ref>{{exclaim}} case insensitive <pre> #Apple 2 #電影 1 #追劇 1 #藍芽 1 #綜藝 1 </pre> Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -c | sort -nr | awk ' { t = $1; $1 = $2; $2 = t; print; } '</nowiki>}} {{exclaim}} case sensitive <pre> #電影 1 #追劇 1 #藍芽 1 #綜藝 1 #apple 1 #Apple 1 </pre> === Input Format B: Multiple terms per line === {{exclaim}} Each line contains multiple terms/keywords separated by spaces file: input.txt <pre> 電影 追劇 綜藝 藍芽 apple 電影 電影 綜藝 </pre> ==== Method using awk for word frequency counting ==== {{kbd | key=<nowiki>awk '{for(i=1;i<=NF;i++) count[$i]++} END {for(word in count) print count[word], word}' input.txt | sort -nr</nowiki>}} Output: <pre> 3 電影 2 綜藝 1 追劇 1 藍芽 1 apple </pre> How it works: * {{kbd | key=<nowiki>{for(i=1;i<=NF;i++) count[$i]++}</nowiki>}} - Loop through each field (word) in each line and increment its count * {{kbd | key=<nowiki>END {for(word in count) print count[word], word}</nowiki>}} - After processing all lines, print count and word for each unique word * {{kbd | key=<nowiki>sort -nr</nowiki>}} - Sort numerically in descending order === Verification of count occurrence === <pre> cat test.txt | grep -i "#apple$" | wc -l # or cat test.txt | grep -iw "#apple" | wc -l </pre> Options<ref>[https://en.wikibooks.org/wiki/Grep Grep - Wikibooks, open books for an open world]</ref> * {{kbd | key=<nowiki>-i</nowiki>}} means {{kbd | key=<nowiki>Ignore uppercase vs. lowercase.</nowiki>}} * {{kbd | key=<nowiki>-w</nowiki>}} means {{kbd | key=<nowiki>--word-regexp</nowiki>}}
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information