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: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: | < | ^ Type: | < | ||
| - | --2016-06-07 09: | + | --2016-06-07 09: |
| + | re.txt | ||
| Resolving ocw.mit.edu (ocw.mit.edu)... 23.15.135.8, | Resolving ocw.mit.edu (ocw.mit.edu)... 23.15.135.8, | ||
| Connecting to ocw.mit.edu (ocw.mit.edu)|23.15.135.8|: | Connecting to ocw.mit.edu (ocw.mit.edu)|23.15.135.8|: | ||
| Line 379: | Line 380: | ||
| </ | </ | ||
| ^ What are the first 10 lines of this file? || | ^ What are the first 10 lines of this file? || | ||
| - | ^ Type: | < | + | ^ Type: | < |
| 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: | ||
| << | << | ||
| </ | </ | ||
| + | ^ 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' | ||