General Startup To use vi: vi filename To exit vi and save changes: ZZ or :wq To exit vi without saving changes: :q! To enter vi command mode: [esc] Counts A number preceding any vi command tells vi to repeat that command that many times. Cursor Movement h move left (backspace) j move down k move up l move right (spacebar) [return] move to the beginning of the next line $ last column on the current line 0 move cursor to the first column on the current line ^ move cursor to first nonblank column on the current line w move to the beginning of the next word or punctuation mark W move past the next space b move to the beginning of the previous word or punctuation mark B move to the beginning of the previous word, ignores punctuation e end of next word or punctuation mark E end of next word, ignoring punctuation H move cursor to the top of the screen M move cursor to the middle of the screen L move cursor to the bottom of the screen Screen Movement G move to the last line in the file xG move to line x z+ move current line to top of screen z move current line to the middle of screen z- move current line to the bottom of screen ^F move forward one screen ^B move backward one line ^D move forward one half screen ^U move backward one half screen ^R redraw screen ( does not work with VT100 type terminals ) ^L redraw screen ( does not work with Televideo terminals ) Inserting r replace character under cursor with next character typed R keep replacing character until [esc] is hit i insert before cursor a append after cursor A append at end of line O open line above cursor and enter append mode Deleting x delete character under cursor dd delete line under cursor dw delete word under cursor db delete word before cursor Copying Code yy (yank)'copies' line which may then be put by the p(put) command. Precede with a count for multiple lines. :t. will duplicate the line. :t 7 will copy it after line 7. :,+t0 will copy current and next line at the beginning of the file. :1,t$ will copy lines from beginning of the file to the current cursor position, to the end of the file. Put Command brings back previous deletion or yank of lines, words, or characters P bring back before cursor p bring back after cursor Find Commands ? finds a word going backwards / finds a word going forwards f finds a character on the line under the cursor going forward F finds a character on the line under the cursor going backwards t find a character on the current line going forward and stop one character before it T find a character on the current line going backward and stop one character before it ; repeat last f, F, t, T Find and Replace Commands :%s/hello/goodbye/g find hello and replace with goodbye :%s/^\(.*\)\n\1$/g delete duplicate lines :%s/\n/ /g join lines together Miscellaneous Commands . repeat last command u undo last command issued U undoes all commands on one line xp deletes first character and inserts after second (swap) J join current line with the next line ^G display current line number % if at one parenthesis, will jump to its mate mx mark current line with character x 'x find line marked with character x NOTE: Marks are internal and not written to the file. Line Editor Mode Any commands from the line editor ex can be issued upon entering line mode. To enter: type ':' To exit: press[return] or [esc] ex Commands For a complete list consult the UNIX Programmer's Manual READING FILES copies (reads) filename after cursor in file currently editing :r filename WRITE FILE :w saves the current file without quitting :20,40w filename write the contents of the lines numbered 20 through 40 to a new file named filename MOVING :# move to line # :$ move to last line of file SHELL ESCAPE executes 'cmd' as a shell command. :!'cmd' Source:
Awk does a lot more than select a column from a file or an input stream. It can select columns from selected rows. It can calculate totals, extract substrings, reverse the order of fields and provide a whole lot of other very handy manipulations. Whether you squeeze your awk permutations onto the command line or prefer to build them into scripts, the language is clever and versatile and well worth using even as it joins the ranks of middle-aged computer utilities.
If you want to use awk to add up a bunch of numbers formulated as a column in a text file, you can use a one-liner like this:
$ awk ‘{ SUM+=$1 } END { print SUM }’ < nums
That SUM+= operation adds the contents of column one to a running total for each line of input. The sum is only printed at the very end when input is exhausted. You can change $1 to the column of your choice or $NF if you want to add up the rightmost column.If you prefer scripts to the command line, you could use a script like the addcol script below to sum whatever column you choose. You would just pass the column you want to add on the command line as the COL parameter:
$ awk -f addcol COL=3 < numbers
The -f tells awk to run the designated script (addcol) and COL=3 passes “3” as the column number you want to sum.# addcol
BEGIN { SUM=0 }
{
print $COL
SUM += $COL
}
END { print “Sum: ” SUM }
Awk has some grep-like features as well. If you want to operate only on lines that contain some particular text, you can specify that text on the command line like this:$ awk ‘/choose me/’ textfile
You can also combine search options using && (and), || (or), and even negation operators. The command below selects lines that contain both the word “this” and the word “that”. Using ‘/this/ && !/that/’ would select only lines that contain “this” without containing “that”.$ awk ‘/this/ && /that/’ notes
# find the process that started this one
# make sure that this user provided an answer
You can also select content based on its position within your input. In the command below, we only want to see lines eleven and above.$ awk ‘NR > 10’ counts
11 63
12 99
13 63
14 77
15 41
And, of course, there are a lot of other nice little tricks available.
Awk provides some quick and effective filtering and incorporates an easy syntax. Probably the only thing that takes some getting used to is referring to parameters without putting dollar signs in front of them.