User Tools

Site Tools


unix_102

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
unix_102 [2016/06/07 13:23]
peek
unix_102 [2016/06/07 13:57] (current)
peek
Line 353: Line 353:
 ====== Playing Around ====== ====== Playing Around ======
  
-^ Type: | <code>$ wget -O file.txt 'http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespeare.txt'</code> |+^ Make a safe place to play around || 
 +^ Type: | <code>$ mkdir /tmp/playground 
 +$ cd /tmp/playground 
 +</code>
 +^ Get a text file to play around with || 
 +^ Type: | <code>$ wget -O file.txt 'http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespeare.txt' 
 +--2016-06-07 09:25:57--  http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespea 
 +re.txt 
 +Resolving ocw.mit.edu (ocw.mit.edu)... 23.15.135.8, 23.15.135.19 
 +Connecting to ocw.mit.edu (ocw.mit.edu)|23.15.135.8|:80... connected. 
 +HTTP request sent, awaiting response... 200 OK 
 +Length: 5458199 (5.2M) [text/plain] 
 +Saving to: ‘file.txt’ 
 + 
 +100%[======================================>] 5,458,199   1.58MB/  in 3.3s    
 + 
 +2016-06-07 09:26:00 (1.58 MB/s) - ‘file.txt’ saved [5458199/5458199] 
 +</code> 
 +^ How many lines are in the file? || 
 +^ Type: | <code>$ wc -l file.txt 
 +124456 file.txt 
 +</code>
 +^ How many words are in the file? || 
 +^ Type: | <code>$ wc -w file.txt 
 +901325 file.txt 
 +</code>
 +^ What are the first 10 lines of this file? || 
 +^ Type: | <code>$ head -10 file.txt 
 +This is the 100th Etext file presented by Project Gutenberg, and 
 +is presented in cooperation with World Library, Inc., from their 
 +Library of the Future and Shakespeare CDROMS.  Project Gutenberg 
 +often releases Etexts that are NOT placed in the Public Domain!! 
 + 
 +Shakespeare 
 + 
 +*This Etext has certain copyright implications you should read!* 
 + 
 +<<THIS ELECTRONIC VERSION OF THE COMPLETE WORKS OF WILLIAM 
 +</code>
 +^ What is the 3rd word on each line of the last ten lines? || 
 +^ Type: | <code>$ cat file.txt \ 
 +> | awk '{print $3}' \ 
 +> | tail -10 
 +ONLY, 
 +COMMERCIAL 
 +CHARGES 
 + 
 + 
 + 
 +this 
 + 
 + 
 + 
 +</code>
 +^ What are the top 10 most frequently used words? || 
 +^ Type: | <code>$ cat file.txt \ 
 +> | awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" \ 
 +> | sort -nr \ 
 +> | head -10 
 +517065  
 +23242 the 
 +19540 I 
 +18297 and 
 +15623 to 
 +15544 of 
 +12532 a 
 +10824 my 
 +9576 in 
 +9081 you 
 +</code>
 +^ NOTE: How would you know to do that!?!?  The easiest way is to just search online for someone who's already done it, and then copy what they typed.  There are several online forums for command line usage too.  That's what I did.  Awk is so powerful, I've only scratched the surface of it myself. || 
 +^ In the file '/etc/passwd', what is the 8th line? || 
 +^ Type: | <code>$ cat /etc/passwd | head -8 | tail -1 
 +lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin 
 +</code>
 +^ The lines in /etc/passwd are fields separated by a colon.  What is the value in the 5th field? || 
 +^ Type: | <code>$ cat /etc/passwd | head -8 | tail -1 | awk -F: '{print $5}' 
 +lp 
 +</code>
 +^ The 3rd field is the User ID number (UID).  What is the sum of all UIDs in '/etc/passwd'? || 
 +^ Type: | <code>$ n=0 
 +$ cat /etc/passwd \ 
 +> | awk -F: '{print $3}' \ 
 +> | while read d ; do let n=$(( $n + $d )) ; done 
 +$ echo $n 
 +
 +</code>
 +^ | NOTE: That didn't work!  Why?  Because the while loop executes in a subshell, and while it is possible to pass values of exported values from parent shell to child subshell, the child gets a copy and not the original variable.  This means that when the child process sums up values for 'n', that value is lost when the child process exits.  Since the parent's version of 'n' never changes, it's value is still zero. | 
 +^ So what's the correct way to do it?  Here's one way that works: || 
 +^ Type: | <code>$ n=0 
 +$ for d in $(cat /etc/passwd | awk -F: '{print $3}') ; do \ 
 +> n=$(( $n + $d )) ; \ 
 +> done 
 +$ echo $n 
 +68740 
 +</code>
 +^ | NOTE: The for loop doesn't execute in a subshell.  How would you know this?  Well, reading the bash manual is probably the best way.  :-/ |
unix_102.1465305785.txt.gz · Last modified: 2016/06/07 13:23 by peek