CS 497C - Homework 5 - Name:_________________ DUE: Monday, November 5 (in class) Please show all work on a separate sheet attached to this sheet. (50 points - 5 points for each problem) + (5 bonus points) 1. Match the filename chapa, chapb, chapc, chapx, chapy, and chapz with one expression. Does rm * remove all files? Ans: 1) chap[a-cx-z] or chap[abcxyz] 2) Not the ones beginning with a dot. 2. Make this setting at the command prompt. Can you execute $x? Why? x='ls | more' or set x='ls | more' (C shell) What is the difference between directory='pwd' and directory=`pwd`. Ans: 1) No, you can't. | and more are considered to be arguments of ls. 2) The first sets the variable directory to the string pwd. The second sets it to the absolute pathname of the current directory. 3. You want to concatenate two files, foo1 and foo2, but also insert some text in between from the terminal. How will you do this? Ans: Use cat foo1 - foo2 > foo3. The shell will pause for input which is appended to foo1. 4. What is a filter? Where does a filter get its input from? Ans: 1) A filter is a command which uses both standard input and standard output. 2) It can accept input from (i) a file with < (ii) a pipeline using |. Most filters accpet input from the keyboard as well. 5. Using command substitution, write a command sequence which always prints calendar of the current month. Ans: The command cal `date "+%m %Y"` or cal `date "+%m 20%y"` prints the calendar of the current month. The date command extracts the month number and year in the form understood by cal. Note: In most UNIX systems, just using "cal" will show the calendar of the current month. 6. Select lines 5 to 10 of a file in two ways by using head and tail. Ans: 1) tail +5 foo | head -6 2) head -10 foo | tail +5 7. Extract the names of the users from /etc/passwd after ignoring the first 10 entries. Ans: The sequence tail +11 /etc/passwd | cut -d: -f1 ignores the first ten entries in /etc/passwd and extracts the first field (the user-id) from the output. 8. Sort the file /etc/passwd on GUID (primary) and UID (secondary) so that the users with same GUID are placed together. Users with a lower UID should be placed higher in the list. Ans: You require to do a numeric sort on the fourth and third fields: sort -t: -n +3 +0 +2 /etc/passwd or sort -t: -n +3 -4 +2 /etc/passwd or sort -t: -n +3 -4 +2 -3 /etc/passwd 9. Devise a command sequence which lists the five largest files in the current directory. Ans: ls -S | head -5 or ls -l | tail +2 | sort -n -r +4 -5 | head -5 or ls -l | tr -s ' ' | sort -t ' ' -nr +4 | head -5 10. List the users logged in more than once. Ans: Cut out the first field from the who output, sort it and then select only the repeated lines: who | cut -d' ' -f1 | sort | uniq -d 11. Invert the names in short list on Page 275 and place a comma after the last name (i.e. last name, firt name). The result should look like: 2233|harris, charles|g.m. |sales |12/12/52| 90000 9876|johnson, bill|director |production|03/12/50|130000 5678|dylan, robert|d.g.m. |marketing |04/19/43| 85000 2365|woodcock, john|director |personnel |05/11/47|120000 5423|wood, barry|chairman |admin |08/30/56|160000 Ans: This requires the use of a series of cut and paste commands. First cut the first, second and remaining fields of shortlist to temporary files: cut -d\| -f1 shortlist > temp1 cut -d\| -f2 shortlist > temp2 cut -d\| -f3- shortlist > temp3 Now, cut the two components of the name from the name field, but offset the first name by one space: cut -d' ' -f1 temp2 > temp2a cut -d' ' -f2 temp2 > temp2b Paste the two names using the comma as delimiter before pasting back all sections: paste -d, temp2b temp2a > temp2 paste -d'|' temp[1-3]