Count occurrences of a word in string: Difference between revisions
Jump to navigation
Jump to search
→BASH
m (Text replacement - "Category:Text file processing" to "Category:String manipulation") |
(→BASH) |
||
| Line 66: | Line 66: | ||
</pre> | </pre> | ||
Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -ic | sort -nr</nowiki>}} {{exclaim}} insensitive | === Output format I: occurrence & keyword === | ||
{{exclaim}} The term each line 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> | <pre> | ||
2 #Apple | 2 #Apple | ||
| Line 75: | Line 78: | ||
</pre> | </pre> | ||
Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -c</nowiki>}} {{exclaim}} sensitive | Result of the execution of command: {{kbd | key=<nowiki>sort test.txt | uniq -c</nowiki>}} {{exclaim}} case sensitive | ||
<pre> | <pre> | ||
1 #Apple | 1 #Apple | ||
| Line 85: | Line 88: | ||
</pre> | </pre> | ||
=== Output format II: keyword & occurrence === | |||
{{exclaim}} The term each line 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> | |||
=== Verification of count occurrence === | |||
<pre> | <pre> | ||
cat test.txt | grep -i "#apple$" | wc -l | cat test.txt | grep -i "#apple$" | wc -l | ||