This shows you the differences between two versions of the page.
| 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: | < | + | ^ Make a safe place to play around || |
| + | ^ Type: | < | ||
| + | $ cd / | ||
| + | </ | ||
| + | ^ Get a text file to play around with || | ||
| + | ^ Type: | < | ||
| + | --2016-06-07 09: | ||
| + | re.txt | ||
| + | Resolving ocw.mit.edu (ocw.mit.edu)... 23.15.135.8, | ||
| + | Connecting to ocw.mit.edu (ocw.mit.edu)|23.15.135.8|: | ||
| + | HTTP request sent, awaiting response... 200 OK | ||
| + | Length: 5458199 (5.2M) [text/ | ||
| + | Saving to: ‘file.txt’ | ||
| + | |||
| + | 100%[======================================> | ||
| + | |||
| + | 2016-06-07 09:26:00 (1.58 MB/s) - ‘file.txt’ saved [5458199/ | ||
| + | </ | ||
| + | ^ How many lines are in the file? || | ||
| + | ^ Type: | < | ||
| + | 124456 file.txt | ||
| + | </ | ||
| + | ^ How many words are in the file? || | ||
| + | ^ Type: | < | ||
| + | 901325 file.txt | ||
| + | </ | ||
| + | ^ What are the first 10 lines of this file? || | ||
| + | ^ Type: | < | ||
| + | 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. | ||
| + | often releases Etexts that are NOT placed in the Public Domain!! | ||
| + | |||
| + | Shakespeare | ||
| + | |||
| + | *This Etext has certain copyright implications you should read!* | ||
| + | |||
| + | << | ||
| + | </ | ||
| + | ^ What is the 3rd word on each line of the last ten lines? || | ||
| + | ^ Type: | < | ||
| + | > | awk ' | ||
| + | > | tail -10 | ||
| + | ONLY, | ||
| + | COMMERCIAL | ||
| + | CHARGES | ||
| + | |||
| + | |||
| + | |||
| + | this | ||
| + | |||
| + | |||
| + | |||
| + | </ | ||
| + | ^ What are the top 10 most frequently used words? || | ||
| + | ^ Type: | < | ||
| + | > | awk ' | ||
| + | > | sort -nr \ | ||
| + | > | head -10 | ||
| + | 517065 | ||
| + | 23242 the | ||
| + | 19540 I | ||
| + | 18297 and | ||
| + | 15623 to | ||
| + | 15544 of | ||
| + | 12532 a | ||
| + | 10824 my | ||
| + | 9576 in | ||
| + | 9081 you | ||
| + | </ | ||
| + | ^ NOTE: How would you know to do that!? | ||
| + | ^ In the file '/ | ||
| + | ^ Type: | < | ||
| + | lp: | ||
| + | </ | ||
| + | ^ The lines in /etc/passwd are fields separated by a colon. | ||
| + | ^ Type: | < | ||
| + | lp | ||
| + | </ | ||
| + | ^ The 3rd field is the User ID number (UID). | ||
| + | ^ Type: | < | ||
| + | $ cat /etc/passwd \ | ||
| + | > | awk -F: ' | ||
| + | > | while read d ; do let n=$(( $n + $d )) ; done | ||
| + | $ echo $n | ||
| + | 0 | ||
| + | </ | ||
| + | ^ | 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. | ||
| + | ^ So what's the correct way to do it? Here's one way that works: || | ||
| + | ^ Type: | < | ||
| + | $ for d in $(cat /etc/passwd | awk -F: ' | ||
| + | > n=$(( $n + $d )) ; \ | ||
| + | > done | ||
| + | $ echo $n | ||
| + | 68740 | ||
| + | </ | ||
| + | ^ | NOTE: The for loop doesn' | ||