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:29]
peek [Playing Around]
unix_102 [2016/06/07 13:57] (current)
peek
Line 359: Line 359:
 ^ Get a text file to play around with || ^ 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' ^ 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.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 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. Connecting to ocw.mit.edu (ocw.mit.edu)|23.15.135.8|:80... connected.
Line 379: Line 380:
 </code> | </code> |
 ^ What are the first 10 lines of this file? || ^ What are the first 10 lines of this file? ||
-^ Type: | <code>head -10 file.txt+^ Type: | <code>head -10 file.txt
 This is the 100th Etext file presented by Project Gutenberg, and This is the 100th Etext file presented by Project Gutenberg, and
 is presented in cooperation with World Library, Inc., from their is presented in cooperation with World Library, Inc., from their
Line 391: Line 392:
 <<THIS ELECTRONIC VERSION OF THE COMPLETE WORKS OF WILLIAM <<THIS ELECTRONIC VERSION OF THE COMPLETE WORKS OF WILLIAM
 </code> | </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
 +0
 +</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.1465306143.txt.gz · Last modified: 2016/06/07 13:29 by peek