cs104 lab 4 alias, redirection, pipes, head/tail/cut/tr I. alias, ; Type %alias xyz=ls and test it with %xyz and %alias . The alias command is part of the shell, so is under %info bash->bash features->alias and %info bash-> shell built-in commands -> bash builtin commands. Use alias to make it so "hh" moves to your home directory. Hmmm... it seems to have a problem with spaces (%alias to see what happened.) ""'s can be used to fix it. Use alias so "getsuba" will copy suba.dat (from lab2/sec1) to wherever you are now. Then turn them off and check they are really gone. It looks like ls has an alias. Get rid of that alias and see what the "real" ls does. Try typing ls with that "--color" option. Type %ls;ls to see with ; does. Now try %alias lsx2=ls;ls Hmmm... it thought the second ls was a separate command. Again, ""'s can fix it. Try to get lsx2 to work properly. Now that we can use ; for two commands, use alias and make it so %restartlab2 gets a fresh copy of lab2 in our home directory (same as we did in lab2 -- an rm and a cp.) II. output/error redirection Let's make a new file errors.txt with a long line of X's, without using pico. Try: %cat > errors.txt . Recall this reads from the input and that ^D (on a line by itself) makes it think the input file is done. Try the same trick to make file Y with just a "y" in it. Try these redirections. X shouldn't exist (so we can see what happens to the error message.) You may want to clean out errors.txt in-between each: %cat X Y > errors.txt %cat X Y 2> errors.txt %cat X Y > 2> errors.txt -- just a cool error, next is correct way: %cat X Y >errors.txt 2> errors.txt -- hmmm, not quite %cat X Y >>errors.txt 2>>errors.txt -- ah-ha! Use redirection (no pico or cut&paste!) to make errors.txt have the error messages you get from doing cat, rm, ls and cd on nonexistant file X (%rm X, etc... .) Let's try to add the line "ERRORS" to the top of that file, without an editor. Make err2.txt (no editor) with ERRORS it in, then use cat and redirection to combine err2.txt and errors.txt into a new file. III. head/tail, pipes Make file abc with 12 lines (a-l?) and try "%cat -n abc > def". def should now be numbered. We'll use it to test head and tail. Try: %head def %head -n3 def -- not very exciting %tail def %tail -n3 def %tail -n+3 def %tail -n-3 def -- from top/this many We know abc has 12 lines in it, but let's say we didn't and wanted to check. Write a command which will print the last numbered line of abc (number it, then print the last line. You'll need to use a pipe |.) Use head, tail and a pipe to print lines 4-6 (d,e,f) of abc. IV. cut, tr a) For the length of a file, we don't want to see the line, just the number. Use cut (with the -c option) to show just the line number of the last line of the file (this will use | twice, with the cut last.) ~cs104/labs/lab4/cutfile.txt has some names with ,'s and :'s. Use cut with the -f option and delimiter set to : to pick out the 1st and 3rd names on each line. It won't be useful, but try the same thing with comma as the delimiter. To find the last name of person X, we can use cut twice in a row: once to get that person (with :) and again with , to get the last name (using a pipe.) Write a command to get the last names of a column (for the second column, you should get: Jackson Gamgee Bunny) and write it to colXnames.txt (more redirection.) b) tr Try these: %cat cutfile.txt | tr o X "%cat cutfile.txt | tr oa X- tr stands for translate. What does the order of the letters mean? The default delimiter for cut -f is a TAB. Instead of changing the delimter to : in part a, let's use tr to first change the :'s to TABS. Recall, tabs are escaped as \t. tr allows a range. Try %cat cutfile.txt | tr b-e 6-9 . This is the same as writing it out the long way. Use this trick to show the last names of everyone in the first column of cutfile in capital letters. Our "number of lines in a file" pipe from III and IV a) gives answers like " 87". We'd like to get rid of those leading spaces, so the answer is just a number. Look up the "delete" option for the tr command (man, and/or info -> squeezing.) Use it to improve the "number of lines in file" pipe.