============================================== Unix Lab 3 (Live demo of command line editing) ============================================== - Prepared by Ian Allen - idallen@ncf.ca Log in to ACADAIX. Enable vi-mode command line editing in your shell (page 608). Make sure you use the h,j,k,l keys, not the arrow keys! Work along with your instructor solving the following problem: "Your boss wants you to provide a sorted and nicely-paginated list of unique names for all the accounts currently on the ACADAIX computer. The names should be sorted by Last name, not First name. Put the header "Sorted Files" on the output pages." Work with your instructor doing this iterative solution: Look at the accounts in the Password File. $ more /etc/passwd Read the man page. $ man /etc/passwd Learn that the fifth field (Gcos) has the "other info" such as name in it. Use more to scan the passwd file to verify that this is true. Decide to extract the fifth field. $ cut -f5 /etc/passwd | more Doesn't work. Why? Read the man page for cut. $ man cut Learn that the default field delimiter is a TAB, not a colon. Learn how to use a different delimiter. $ cut -d: -f5 /etc/passwd | more That works, but it has too many blank lines and the names are not in sorted order. Try sorting it. $ cut -d: -f5 /etc/passwd | sort | more Still too many blank lines and duplicate names. Use "uniq". $ cut -d: -f5 /etc/passwd | sort | uniq | more Better, but it's sorting on first name, not last name. Read the sort man page. $ man sort Learn how to sort on the second field. $ cut -d: -f5 /etc/passwd | sort +1 | uniq | more Discover that many names are separated by commas, not blanks. Need to translate commas to blanks before sorting. $ cut -d: -f5 /etc/passwd | tr ',' ' ' | sort +1 | uniq | more Better, but all the lower-case names appear after the upper-case names. I want them to sort together. Read the man page for sort and look for "upper". Learn that "-f" disregards case in sorting. $ cut -d: -f5 /etc/passwd | tr ',' ' ' | sort +1f | uniq | more Good. Now it needs to be paginated nicely. Use the "pr" command: $ cut -d: -f5 /etc/passwd | tr ',' ' ' | sort +1f | uniq | pr | more He wants a header "Sorted Files" on the output pages. Read the man page for "pr" and learn about "-h" for a header. $ cut -d: -f5 /etc/passwd | tr ',' ' ' | sort +1f | uniq | pr -h "Sorted Files" | more Done! Before you're quite finished, the boss calls up and asks you how many unique names there were. You get the boss a quick answer with this: $ cut -d: -f5 /etc/passwd | tr ',' ' ' | sort +1f | uniq | wc The boss then asks you for a second list of names that are the names that appear as single words, i.e. the account user didn't specify a last name. You produce this answer right away: $ cut -d: -f5 /etc/passwd | tr ',' ' ' | sort +1f | uniq | grep -v " " You're becoming a Unix guru!