Title: INTRODUCTION TO UNIX
1INTRODUCTION TO UNIX
- INTRODUCTION TO THE UNIX OPERATING SYSTEM
- SESSION 3
2BASIC EDITING WITH VI AND EMACS
3Learning to Use the Text Editors
- UNIX systems generally provide three text editors
as standard parts of the operating system release - vi, the visual editor
- ed, a line oriented editor
- sed, a stream oriented editor
4Text editing tools in UNIX
- Designed for a terse, very fast user interface
and operation that favors the skilled expert - emacs, which is not provided as standard, is
slightly easier to learn and work with - vi, the standard full-page editor, should be
learned since it can be found on any system
5The vi Text Editor
6Setting your terminal type
- vi is terminal-independent so it can be used on
any terminal type - to let vi know what type of terminal is being
used, the TERM environment variable must be set
properly - TERMansi
- export TERM
7Setting your terminal type
- the TERM variable is generally set when the user
logs in by the .profile shell script - vi uses the TERM entry to locate the correct
control sequences to send to the terminal by
looking at the terminfo data base,
usr/share/lib/terminfo or /etc/termcap on older
releases of UNIX
8Setting your terminal type
- 80386/80486 based UNIX systems use TERMAT386 (or
TERMat386) for their system console - X window systems generally use TERMxterm for
proper operation
9Starting the vi editor
- vi can be started from the shell in the following
fashions - vi old.file
- vi new.file
- vi file1 file2
- vi
10Starting the vi editor
- vi may also be opened to any specific line number
of an existing file or with any vi command to be
interpreted through the use of the "" option - vi 45 old.file
- vi old.file
- vi /textstring old.file
- the screen cursor marks the place of the current
line and current position
11Modes in vi
- vi has three modes of operation
- command mode
- input or text mode
- last line mode
12Modes in vi
- the option "showmode" may be turned on to display
the current mode on the screen so that input
errors are less likely - set showmode
13Switching modes
- last line mode may be activated from command mode
by entering "" and when the command entered is
completed vi returns to command mode - command mode may be activated from text or input
mode by pressing the ESCAPE key
14Switching modes
- text or input mode may be entered from command
mode through a variety of commands
15Ending your vi session
- to exit from vi, the command "q" may be entered
while in last line mode current changes to the
file may be abandoned by using the "!" (bang)
after the exit command "q!"
16Writing files
- files are written from the vi buffer to the disk
by the "w" command - new files may be named and existing files may be
saved under another name by using the write
command as follows "w newfile
17Writing files
- the write and quit commands may be combined to
save a file and exit the editor in one operation
as follows "wq" - The command "ZZ" may be used to write a file and
quit the editor when in command mode
18Reading files
- the "r" command may be used in last line mode to
read a existing file into an empty buffer or to
add the contents of the file to the existing
buffer - line numbers may be added to the command to read
in the desired portions of the file to the buffer
19Changing files
- the "n" command closes the current file and
loads the next file in the vi command argument
list to the edit buffer - only one file at a time may be edited and the
current file is closed when the next file is
loaded all changes must be saved or abandoned at
that point
20Redrawing the screen
- the CTRL-L operator may be used to force vi to
redisplay the current screen in the event that it
becomes corrupted
21Escaping to the shell
- the "!" operator may be used in last line mode to
execute a command in a subshell - !ls
- !sh
22Escaping to the shell
- while the command is executing, vi is temporarily
suspended but it will resume following the
command - a subshell may be executed and all UNIX
operations can be done while vi waits to resume
23Configuring vi options
- the vi editor has many options that can be used
to change its behavior and make it a bit easier
to use
24Configuring vi options
- the "set" command can be used to temporarily set
options within a current vi session - set all (shows all available options)
- set showmode
- set noshowmode ("no" turns off option)
- set window10
- set number (displays line numbers)
25Configuring vi options
- the environment variable EXINIT may be used in
the .profile shell script to provide the "set"
commands to vi when it starts up - EXINIT'set number showmode' export EXINIT
26Editing in vi
- Undoing a change
- the "u" operator may be used in command mode to
undo the last change made to the file - only the most recent change is saved by the editor
27Entering input mode from command mode
- The following operator may be used from command
mode to switch to text or input mode - i enter text before cursor
- a enter text after cursor
- I enter text at start of line
- A enter text at end of line
- o open line above and enter text
- O open line below and enter text
28Command mode
- command mode is entered from text mode by
pressing the ESCAPE key
29Moving around in the buffer
- the following operators may be used to move
around the text buffer while in command mode - l move one space right
- L move to the last line on the screen
- h move one space left
- H move to the top line of the screen
30Moving around in the buffer
- the following operators may be used to move
around the text buffer while in command mode - j move one line down
- k move one line up
- move to the end of the line
- move to the start of the line
31Moving around in the buffer
- the following operators may be used to move
around the text buffer while in command mode - b jumps to beginning of previous word
- w jump to the next word
- e jump to the end of word
- G jump to the end of the buffer
32Moving around in the buffer
- the following operators may be used to move
around the text buffer while in command mode - CTRL-d scroll forward
- CTRL-u scroll backward
- CTRL-f next screen
- CTRL-b previous screen
33Searching for text
- a variant of last line mode allows you to search
for text strings in a file being edited - enter a '/' for a forward search or a '?' for a
backward search and the text string for the
search - /search.string
34Changing text
- the commands 'r', 'R', 'C', and 'J' are used to
change existing text - 'r' replaces a single character leaving you in
command mode - 'R' lets you replace as many characters as you
wish starting at the current cursor position,
leaving you in input mode
35Changing text
- the commands 'r', 'R', 'C', and 'J' are used to
change existing text - 'C' lets you change the whole line
- 'J' merges two lines of text by deleting the
newline character at the end of the current line
36Deleting text
- vi provides a number of operators for deleting
text - The 'x' operator deletes the current character
the 'dd' operator deletes the current line the
'dw' operator deletes the current word the 'cw'
operator is used to change the current word the
'cc' operator changes the current line the 'D'
operator deletes from the cursor position to the
end of the line
37Deleting text
- vi provides a number of operators for deleting
text - numbers of lines and ranges of numbers may be
used with all of these commands - 3,5d
- 6dd
- 5dw
38Repeating a change
- The '.' dot command applies the last change to
the current cursor position - This includes text addition and deletion
operations as well as replacements
39Cut and paste operations
- Blocks of text may be pulled out of a file into a
buffer and then replaced into the where desired - the 'Y' command yanks text lines out of the file
and the 'p' command places them back into the
file after the current line the 'P' command
places them back just above the current line
40Cut and paste operations
- text deleted with commands like 'dd' may be
replaced at the current cursor position using 'p'
or 'P' - the 'u' command replaces text at the position it
was taken from (undo)
41Cut and paste operations
- buffers may be named with a single letter to
store multiple text strings - 7"aY
- and replaced with
- "ap
42Cut and paste using line numbers
- Last line mode provides commands to move and copy
blocks of lines by number using the 'm' operator
for move and the 't' operator for copy - 3,5m9
- 3,5t9
- 6t9
43The emacs Editor
44The emacs editor
- Is not part of the standard release of the UNIX
system although many sites make it available to
their users - very different than vi
- always in input mode
- CTRL key oriented commands
- ESC key commands (meta commands)
45Using Editors with the X Window System
46vi and emacs
- are not designed to take full advantage of the X
Windows system - the editor may be used in this environment but
the scrolling features of the X Windows will not
affect text within the editors - care must be taken to set options in the editors
to work with the xterm settings
47REGULAR EXPRESSIONS AND ADVANCED EDITING
48Regular expressions
- A regular expression consists of operators that
describe single characters to search for
anywhere a /string/ is used, a regular expression
may be substituted
49Regular expressions use the following operators
and syntax
- the '.' operator substitutes for any single
character and may be used in combination with
other characters - a.c
- a..
- ..c
50Regular expressions
- the and brackets may be used to define the
set of characters that will match any single
character - abc
- aA
- aAbB
- 0123456789
51Regular expressions
- the and brackets may be used to define the
set of characters that will match any single
character - 0-9
- A-Za-z
- s/aAbB/new string/g
- 1,s/0-9/new string
52Regular expressions
- special characters such as the may be
represented in a regular expression by preceding
them with a \ (backslash) character
53Regular expressions
- two, three, or more regular expressions may be
combined and the '' operator may be used to
indicate zero or more occurrences of the
preceding single character expression - s/0-90-9/new string
- s/./new line
- s/0-90-9/new string/g
54Regular expressions
- the use of the '' operator allows you to add a
string to an existing string of characters
instead of deleting thee existing string - s/0-90-9/add string
- s/a-zA-Za-zA-Zadd string/g
55The 'grep' Command
56The 'grep' (global regular expression and print)
command
- Is a filter which reads std. input or a list of
files given as arguments and writes to std.
output, any lines containing the match string - using the 'grep' command
- grep match.string textfile
- cat textfile grep "a-z12
- cat file1 grep "a-z" gt errorfile
57The 'grep' Command
- options for the 'grep' command
- '-v' select non matching lines
- '-c' count matching lines
- '-n' list line numbers with match string
- '-i' ignore case distinctions
58The 'grep' Command
- the 'egrep' (extended) and 'fgrep' (fixed)
commands provide search capabilities optimized
for different circumstances - the 'fgrep' command only accepts a fixed string
to search for
59The 'grep' Command
- the 'egrep' command provides more regular
expression operators for more complex match
expressions - '' zero or more occurrences
- '' zero or more occurrences
- '?' zero or one occurrence
- '' either of a pair expressions
- '()' use of parenthesis for grouping
- '-f file.name' match expression in file
60The 'sed' Stream Editor
61The 'sed' (stream editor)
- Combines characteristics of both 'ed' and 'grep'
allowing changes to be made in the input file it
is often used with large files which can not be
handled easily by an interactive editor
62The 'sed' (stream editor)
- using the 'sed' command
- sed "s/hello/goodbye/" textfile
- sed "/hello/d" textfile
- sed "s/hello//" textfile
- sed "3,7s/hello//" textfile
- sed "/hello/,/goodbye/s/bad/good/g file
- sed -f commandfile textfile gt outfile
63Basic Editing with 'ed
- The 'ed' command, a line-oriented editor which
serves as the basis for all the other editors
under UNIX
64Using the 'ed' command for line-oriented text
editing operations
- entering 'ed
- ed filename
- 260
- ed newfile
- ?newfile
- ed
65Modes in 'ed
- 'command mode' is used to enter commands to 'ed'
such as writing a file or searching for text
strings you are in this mode when you enter 'ed - 'input mode' is used to enter text into your file
66Turning on prompts and help
- the 'P' operator will turn on an '' prompt when
you are in command mode - ed filename
- 260
- P
67Turning on prompts and help
- the 'H' operator will display a more informative
error message when selected - ed filename
- 260
- P
- H
- l
- line out of range
68Write and quit
- the 'w' operator writes the file you are editing
from the ed buffer to a file, prompting with a
'?' if no file has been named - ed filename
- 260
- P
- w
- 260
69Write and quit
- the 'q' operator allows you to quit the editor if
no changes have been made in the file since the
last save if changes have been made, you will be
prompted for confirmation - ed filename
- 260
- q
70Working with lines
- in command mode the following apply
- '.' current line
- '' last line
- 'p' print current line
- ed textfile
- 260
- p
- last line of file
71Working with lines
- the '' operator tells you the line number when
used with '.' or '' and line numbers may be used
in command mode to indicate positions for
operator actions - ed sixline
- 260
- p
- line six text
72Working with lines
- .
- 6
-
- 6
- 1,2p
- line one text
- line two text
- 3,p
73Working with lines
- line three text
- line four text
- line five text
- line six text
- -1,p
- line five text
- line six text
74Working with lines
- ,p
- line one text
- line two text
- line three text
- line four text
- line five text
- line six text
75Working with lines
- 4
- line four text
- p
- line four text
- q
76Input mode
- the 'a' (append, after current line) and the 'i'
(input, before current line) operators are used
to go into input mode, text may then be typed
into the file - you remain in input mode until using the '.'
operator on an empty line which returns you to
command mode
77Input mode
- the 'a' and 'i' operators may be preceded by a
line number to begin text entry at a specific
place in a file - ed text
- ?text
- a
- line one
78Input mode
- line two
- line three
- .
- ,p
- line one
- line two
79Input mode
80Deleting lines
- the 'd' operator will delete the current line
from the text file it will also accept a line
number or range for deletion - ed text
- ,p
- line one
- line two
- line three
81Deleting lines
82Deleting lines
83Undoing mistakes
- the 'u' (undo) operator allows you to undo the
last change made to the file and restores the
buffer to its previous state - using the 'u' operator a second time restores the
buffer to the state it was prior to the first
'u' operator
84Searching for strings
- the '/' operator instructs 'ed' to search for a
string, starting at the current line and
continuing around from the beginning of the file
until the current line is reached again a
backwards search may be done using the '?'
operator - a search may be repeated by simply entering a '/'
or '?' followed by a return or enter
85Substituting sections of text
- strings may be replaced within a file using the
's' (substitute) operator followed by a '/', the
text string to replace, a '/', and the
replacement string - the 's' operator may use line numbers or ranges
for its operations otherwise, only the current
line is processed
86Substituting sections of text
- the '' operator is used to indicate the
beginning of a line and the '' operator is used
to indicate the end of a line for the purposes of
preceding or appending text to a line - ed text
- ,p
- text one
87Substituting sections of text
- text three
- 1,s/text/line
- line three
- ,p
- line one
- line three
88Substituting sections of text
- 1,/line //
- ,p
- one
- three
- s//this is line
- this is line three
89Substituting sections of text
- 1s// is enough lines
- one is enough lines
- ,p
- one is enough lines
- this is line three
- w
- q
90Making global changes
- the 'g' (global) operator may be used following
the replacement string (closed by another '/') to
indicate that all occurrences of the target on a
line are to be replaced
91Making global changes
- a "context address" may be substituted for the
target range of this operator or any of the 'ed'
operators providing extremely powerful
capabilities - 1,s/old/new/g
- /start/,/end/s/old/new/g
92Moving and copying lines
- the 'm' command (move) allows you to move a line
or range of lines from one place to another in a
file
93The 't' command (copy)
- allows you to copy a line or range of lines from
one place to another in a file - .m
- 3,5m1
- 2,4t
94Reading in another file
- you may read another file into the edit buffer
with the 'r' (read) operator this will insert
the file after the current line unless a line
number is user with the operator - r oldfile
- 0r oldfile
- 23r oldfile
95Shell escapes, the '!' operator
- the '!' operator allows you to execute any shell
command from within 'ed' including a subshell if
needed the subshell may be terminated with the
CTRL-D sequence and you will be returned to the
editor - !cat textfile
- !sh
96MORE USEFUL GENERAL PURPOSE COMMANDS
97The Environment Revisited
- Exporting environmental variables to subprograms
and shells - the 'export' command
- export
- export LOGNAME
- export PATH
- export TZ
- export TERM
98The 'PATH' variable
- echo PATH
- /usr/hal/bin/bin/usr/bin/usr/ldbin
- PATH/etc/usr/bin
- export PATH
99The 'banner' Command
- The 'banner' command blows up its arguments to
large size and writes them to standard output - using 'banner
- banner HELLO
- banner "job 1
- banner job 1
100The 'clear' Command
- The 'clear' command erases the screen and
displays the '' prompt on the top line - using 'clear
- clear
101The 'date' Command
- The 'date' command displays the current date and
time stored on the system - using the 'date' command
- date
- Wed Jan 18 183000 PST 1989
102The 'cal' Command
- The 'cal' command produces a calendar of the
current month on its standard output - using the 'cal' command
- cal
- cal 1948
- cal 4 1948
103The 'calendar' Command
- The 'calendar' command provides a basic reminder
service taking input from a file named "calendar"
and searching it whenever the command 'calendar'
is entered - The command provides as its standard output, a
listing of the lines in the file which match
today's or tomorrow's date
104Using the 'calendar' command
- calendar
- appointment 1
- appointment 2
105The 'more', 'pg', 'head', and 'tail' Commands
106The 'more' command
- allows you to display a file on the screen and
move forwards and backwards through it - to advance through the screens of text, press the
SPACEBAR or 'F' CTRL-B move backwards through
the text CTRL-L redisplays the current screen
107The 'more' command
- the command will also accept regular expressions
for text searches using 'ed' syntax - 'more' will look at multiple files and skip
forwards and backwards through the list using n
(next) and p (previous) - more file1 file2 file3
108The 'pg' (page) Command
- allows you to display a file on the screen in
similar fashion to 'more - The command also allows you to move around in the
file or the list of files using operators as in
'ed' and regular expressions as in 'grep
109The 'pg' (page) Command
- using the 'pg' command operators at the ''
prompt at the bottom of the screen - h help
- q or Q quit
- ltblankgt or \n next page
- l next line
- d or D display half page more
- . or L redisplay current page
110The 'pg' (page) Command
- f skip next page forward
- n next file
- p previous file
- last page
- w or z set window size display next page
- s savefile save current file in savefile
111The 'pg' (page) Command
- /pattern/ search forward for pattern
- ?pattern? search backward for pattern
- pattern search backward for pattern
- !command execute command
112Using the 'pg' command
- pg textfile1 textfile2
- cat file1 file2 pg
113The 'tail' Command
- looks at the end of the file given as an argument
and writes the last 10 lines of the file to
standard output - using the 'tail' command and its options
- tail /etc/inittab
- tail -6 /etc/inittab
- tail -100c /etc/inittab
- tail -f growing.file
114The 'cmp' command
- The 'cmp' (compare) command will compare any two
files, text or binary, and either list the byte
number of the first difference encountered or
list the byte number and values for each
difference in the files
115Using the 'cmp' command
- cmp file1 file2
- cmp -l file1 file2
- cat file1 cmp -l - file2
116The diff' command
- The 'diff' (differences) command is used for text
files only and produces an index of all lines
that differ between two files, along with their
line numbers and what changes are required to
make the files the same - options for 'diff' include the '-b', ignore
blanks, and the '-e', produce 'ed' script to make
the files the same
117Using the 'diff' command
- diff file1 file2
- cat file3 diff -b - file2
- diff -e file1 file2 gt ed.script
- (cat ed.script echo w) ed file1
118The 'dircmp' command
- The 'dircmp' command compare the contents of two
directory subtrees, reports on files that appear
in one directory only, and compares the files
(using 'cmp') that appear in both directories
119The 'dircmp' command
- it may also be used with the '-d' option to
report the differences - using the 'dircmp' command
- dircmp HOME /tmp/copied
120The 'sort' command
- The 'sort' command is a filter which reorders a
stream of text files into alphabetical order
using the standard ASCII collating sequence,
taking a list of filenames as arguments or
reading standard input it writes the sorted
lines to standard output
121Options for the 'sort' command include
- '-m' merge
- '-n' numeric order
- '-f' ignore uppercase/lowercase differences
- '-d' dictionary, ignore punct. Characters
- '-M' sort on first three characters as a month
field (Jan, Feb, etc.)
122Options for the 'sort' command include
- 'n' select sort key field (delimited by spaces)
by skipping to the next field after the number - '-n' stop sorting on the field just before the
number - '-t' select a different delimiter, ie. -t or
-t - '-b' ignore the "blank" character
- '-r' sort in reverse order
123Using the 'sort' command
- sort datafile
- sort -n datafile
- sort -f textfile
- sort 1 textfile
- sort 3 -4 0 -1 datafile
- sort -t specialfile
124The uniq' Command
- The 'uniq' (unique) command is used to produce a
count of occurrences of a specified line or field
in a file using a sorted file as input and writes
to standard output
125Options for the 'unique' command
- no options - writes one instance of every line
that is different then other lines - -u produces only lines that are not repeated in
the input
126Options for the 'unique' command
- -c count the number of occurrences of the unique
lines in the file - -d produces only lines that are duplicated in
the output - -n ignore the first 'n' fields in its comparison
- n ignore the first 'n' characters of the line
127The 'sort' and 'uniq' Commands
- using the 'uniq' command
- sort datafile uniq
- sort datafile uniq -c
128The 'cut and 'paste' Commands
- The 'cut' command is used to copy only some
fields of an input line to the output while the
'paste' command takes two or more files as input
and reads a line from each file, combines them
into one line using right to left order with a
tab character between the fields, and writes them
to standard output
129Using the cut' command
- cut -b list -n file ...
- cut -c list file ...
- cut -f list -d delim -s file ...
130Using the cut' command
- A mapping of user IDs to names follows
- cut -d -f1,5 /etc/passwd
- cut -d -f1,5 /etc/passwd
- To set name to current login name
- namewho am i cut -f1 -d' '
131Using the 'paste' command
- paste file1 file2
- paste -d data1 data2 gt masterdata
- paste -d12 file1 file2 file3 gt file123
132The 'join' Command
- In contrast to the 'paste' command which joins
corresponding lines from several files, the
'join' command joins lines of files that match on
a specified field
133Using the 'join' command
- join -t file1 file2
- join -j1 n -j2 m file1 file2
134Database Operations for Text Files
- Using the commands and filters discussed thus
far, very complex file manipulations and queries
are possible files may be cut, pasted, sorted,
counted, and output created to meet many data
processing needs
135APPENDIX I
- The following slides summarizes the major vi
commands
136Basic commands for the 'vi' editor
- Enter Input Mode
- i before cursor
- a after cursor
- I at start of line
- A at end of line
- o open line above
- O open line below
- w next word
137Basic commands for the 'vi' editor
- Move Cursor
- l one space right
- h one space left
- j one line down
- k one line up
- end of line
- start of line
138Basic commands for the 'vi' editor
- Delete
- e end of word
- dw delete word
- dd delete line
- D to end of line
- x char at cursor
- C to end of line
139Basic commands for the 'vi' editor
- Change
- cw change word
- cc change line
- Other Functions
- r char at cursor
- u undo
- / search forward
140Basic commands for the 'vi' editor
- Screen Control
- ? search backward
- n next occurrence
- . repeat last action
- Y yank line
- p put below
- P put above
- ZZ write and quit
141Basic commands for the 'vi' editor
- Screen Control
- ESC cancel command
- CTRL-d scroll forward
- CTRL-u scroll back
- CTRL-f next screen
- CTRL-b previous screen
- CTRL-l redraw screen
142Basic commands for the 'vi' editor
- Last Line Mode
- w write file
- q quit
- wq write and quit
- n next file
- r read file
143Basic commands for the 'vi' editor
- Last Line Mode
- e edit file
- f file name
- set change options
- ! shell escape
- n line n
144Basic commands for the 'vi' editor
- When in Input Mode
- BACKSPACE delete char
- CTRL-w delete word
- ESC command mode