# # # # ### # # # ###### # # # # ## # # # # # # # # ## ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ### ####### # # # # # # # ## # # # ### # # # # # # ##### # # ### # # ### # # ###### # # ------------------------------------------------------------------------------- About this document. This document covers a few UNIX _administrator_ tools and concepts. This document was first written by Alex Batko in 1999, then updated in 2000. This document is licensed under the GNU Free Documentation License: http://www.gnu.org/copyleft/fdl.html This document along with others is available at the following URL: http://www.cs.mcgill.ca/~abatko/computers/unix/seminars/ Please refer all complaints and comments to: "Alex Batko" ------------------------------------------------------------------------------- ed. ed is an interactive line editor. vi or vim are able to edit and display a part of a file on the screen, whereas ed is capable of editing and displaying only a line of a file on the screen. ed is usefull to know because it is the underlying editor of vi and vim. The number of lines that ed can store is directly related to how much primary memory is on the computer that you are using ed on. % ed - Start ed, the interactive line-oriented editor. a - (a)ppend lines to the text. hello there . - A single period marks the end of the additions. w foo - Write the lines of text entered to file foo. q - (q)uit ed, and return to the command prompt. Another way to quit ed is to type Ctrl-d. $a - Add one or more lines at the end of the file. If you type a command, and ed responds with ? this means that it has detected an error. At this point you should attempt to reissue the command properly. f - Ask ed what (f)ile is being edited. p - (p)rint the contents of the current line. 3p - (p)rint the contents of line 3. 2,4p - (p)rint the contents of lines 2 through 4. i - (i)nsert text into the line above the current line. 2i - (i)nsert text into line 2. The $ symbol represents the last line number of the file. 1,$p - (p)rint the contents of lines 1 through to the last. Note that symbols in ed have particular meanings based on context. For instance the . symbol on its own means the end of insert mode; whereas it can also represent the current line number. 2,.p - (p)rint the contents of lines 2 through to the current line. .+3p - (p)rint the third line after the current line. .-2p - (p)rint the second line preceding the current line. = - Ask ed how many lines are in the file being edited. .= - Ask ed for the current line number. 3d - (d)elete line 3. 3,5d - (d)elete lines 3 through 5. If you've made changes to a file, and wish to quit without saving, issue command q twice, or simply issue Q. q - Issuing the quit command without saving produces `?'. q - Entering it again verifies that you are aware of the consequence, and (q)uits without saving. Q - (Q)uit without saving. One of the most usefull commands gives the ability to substitute portions of text. The substitute command s has several components: the range to search, the command s, the portion of text to replace, what to substitute that text with, and finally some optional commands. Back slashes mark the beginning and end of each pattern. Note that both patterns must be basic regular expressions. If none of the lines in the document contain the pattern specified, ed will return with `?'. 1,$s/pattern1/pattern2/p - This substitution will search the whole file for pattern1 and substitute it for pattern2. Finally, it will (p)rint the substituted line. 3s/hell/hello/p - Replace pattern `hell' on line 3 by substituting it with pattern `hello', then print the line. s/hell/hello/g - Make a (g)lobal (s)ubstitution of `hell' to `hello'. (g)lobal means that the substitution is to occur to all instances of the pattern per line. 1,$s/hell/hello/ - Issue a substitution to the whole document, but only make one change per line. It is often helpful to be able to recover to a state prior to the current. u - (u)ndo the most recently executed modification. Movement through a working file can be done using `-' and `+'. - - Move to the line above the current line. + - Move to the line below the current line. --- - Move to the third line above the current line. ++++ - Move to the fourth line below the current line. It is possible to execute UNIX commands directly from ed's command mode. Use the ! command to do so. ! date - Run the UNIX command `date'. ! ls -la - Run the UNIX command `ls -la'. ------------------------------------------------------------------------------- sed. sed is a stream editor. It is not meant for creating text; rather is a tool for performing large, intricate transformations to a file. sed reads input text one line at a time and applies a set of editing commands to each line. sed and ed use almost the same commands, and locate lines in similar ways. sed does not change the original file, instead it sends each modified line to standard output. A command synopsis follows sed [-n] [-e commands] [-f scriptfile] file.input [> file.output] The command line option -n changes sed's printing behaviour; sed prints only the lines specified by the p command. Recall that the (p)rint command prints the line(s) to which it is applied. The -e option tells sed that commands follow imm(e)diately. The -f option tells sed that the commands are contained in the file 'scriptfile'. file.input is the input file that sed must edit. The optional '> file.output' is a way to redirect the sed's output to a file called file.output. % sed '' F - Run sed on file F, applying no commands. This prints file F. % sed '1,9d' F - (d)elete lines 1 through 9 to the end of a file F. Thus the output will be the remaining lines. % sed -e 's/\(.*\)=\(.*\)/\2=\1' F - Run sed on file F, applying a substitution. This substitution in particular will swap the first and second bracketed elements around the = symbol. ------------------------------------------------------------------------------- awk. awk is a programming language for text processing. mawk (new awk) is an interpreter for awk. An awk program is a either or both of two elements: a pattern and an action. The synopsis of the awk command follows awk [-f programfile] [-F rs] ['program'] [parameters] [filename(s...)] The -f flag specifies the program file containing a set of patterns. The -F flag is followed by a record separator, which is what awk uses to distinguish records it reads. Note that the space and tab characters are the default input-field separators, so white space normally separates fields. The program is contained within single quotes to avoid shell interpretation. Depending on the system, and thus implementations of awk, numerous parameters may be specified. Finally, the file(s) which awk should operate on may be listed, otherwise awk will processes what it reads from standard input. awk's output will go to standard output. An awk program consists of one or more statements of the form: pattern { action } The action must be enclosed in curly braces in order to distinguish it from the pattern. Note that if the pattern is missing, awk will print the matching line. If { action } is omitted, then it is implicitly { print }. % awk '' F - Print every line of the input file. % awk '{ print }' F - Print every line of the input file. % awk '{ print $1 }' F - Print field number 1 in file F. % awk -F : '{ print $3"\t"$1 }' F - Print field number 3, followed by a tab, followed by field number 1. % awk '{ sum+=$1; print$1"\t"sum }' F - Sum the first field of each line, displaying the original field, with the running total on its right. Awk recognizes the following arithmetic operators: < - less than <= - less than or equal to == - equivalent != - not equal >= - greater than or equal to > - greater than % awk '$3 >= 50' F - Print those lines whose field 3 is greater than or equal to 50. Awk recognizes the following boolean operators: || - OR && - AND ! - NOT % ps auxwww | awk ' ($1 == "abatko") && \ ($2 < 30500) && ($2 > 30000) { print $2 }' | kill - Run ps, parse out processes of interest, and send them to kill. -------------------------------------------------------------------------------