mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			35 lines
		
	
	
		
			991 B
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			35 lines
		
	
	
		
			991 B
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Write a bash script to calculate the frequency of each word in a text file <code>words.txt</code>.</p>
 | 
						|
 | 
						|
<p>For simplicity sake, you may assume:</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>words.txt</code> contains only lowercase characters and space <code>' '</code> characters.</li>
 | 
						|
	<li>Each word must consist of lowercase characters only.</li>
 | 
						|
	<li>Words are separated by one or more whitespace characters.</li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p><strong>Example:</strong></p>
 | 
						|
 | 
						|
<p>Assume that <code>words.txt</code> has the following content:</p>
 | 
						|
 | 
						|
<pre>
 | 
						|
the day is sunny the the
 | 
						|
the sunny is is
 | 
						|
</pre>
 | 
						|
 | 
						|
<p>Your script should output the following, sorted by descending frequency:</p>
 | 
						|
 | 
						|
<pre>
 | 
						|
the 4
 | 
						|
is 3
 | 
						|
sunny 2
 | 
						|
day 1
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><b>Note:</b></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li>Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.</li>
 | 
						|
	<li>Could you write it in one-line using <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html">Unix pipes</a>?</li>
 | 
						|
</ul>
 |