COMP 201 OpenSource L4 ViFileproc
COMP 201 OpenSource L4 ViFileproc
SYSTEM ADMINISTRATION
DANIEL OBUOBI, DCSIT, CU
Vi & File Processing
Using the vi Editor
The vi Editor
Objectives
After studying this lesson, you should be
able to:
– Create and edit simple documents using
the vi editor
Using Operating System Editors
• Starting:
– vi paint begin editing the file paint
– vi you can name the file later
Starting and Quitting vi (Contd)
• vi equivalent
– Page forward one screen (Ctrl-F)
– Page backward one screen (Ctrl-B)
– Start of the current line 0 (zero)
– End of the current line $
– First line of the file 1G
– Last line of the file G
Inserting Text
• The insert commands places vi in insert mode. To use any of these
commands, you must first be in command mode. Remember to press Esc
to make sure you are in command mode.
• Insert
– i insert text to the left of the cursor (This is the command you
will use most often)
– I insert text at the beginning of a line
• Append
– a insert text to the right of the cursor (This command is most
useful when your cursor is at the end of a line and you wish to add more
text)
– A add text to the end of a line
• Open line
– o open a line below the current cursor position
– O open a line above the current cursor position
Changing Text
• Changing a Word cw
– Position the cursor at the beginning of the word to be replaced, type cw,
followed by the new word. To finish, press Esc.
– To change part of a word, place the cursor on the word, to the right of
the portion to be saved. Type cw, type the correction, and press Esc.
• Substituting Character(s) s
– To substitute one or more characters for the character under the cursor,
type s, followed by the new text. Press Esc to return to command mode.
• Replacing One Character r
– Position the cursor over the character and type r, followed by just one
replacement character.
– After the substitution, vi automatically returns to command mode (you
do not need to press Esc).
Redo and Undo
• Repeating a change
– . (period)
• Undo a command
–u
– If you make a mistake, you can "undo" the
mistake by immediately issuing the undo
command by entering u.
Deleting Text
• The delete commands delete the character, word, or line
you indicate. vi stays in command mode, so any
subsequent text insertions must be preceded by
additional commands to enter insert mode.
• :set showmode
– Put a message at the bottom right corner of
your screen, indicating insert, append,
replace, change, or open modes. There is no
message if you are in the command mode.
– Note: This feature does not work on all
versions of UNIX
– Abbreviate the command :set smd
Going to a Specific Line
:[range]s/old_string/new_string[/option]
• Where
anything enclosed in [ ] is not mandatory
: is the colon prefix for the last line command
range is a valid specification of lines (if omitted, the
current line is the range)
Substituting Text (Contd)
:[range]s/old_string/new_string[/option]
s is the syntax of the substitute command
old_string is the text you want to replace
/ is a delimiter for replacement
new_string is the new text
/option is a modifier to the command, g for
global, c for confirm
Examples of Substituting Text
More Examples
• :s/big/small/c
– replace “big” with “small” (first occurrence of big)
on the current line but confirm the replacement.
– type y, press Enter to confirm
– type n, press Enter to ignore
• :1,$s/dog/cat/gc
– replace “dog” with “cat” (every occurrence) for
the entire file but confirm each replacement
Insert One File Into Another
• By default
– stdin is associated with keyboard
– stdout is associated with screen
– stderr is associated with screen
• <
– Redirect input, read input from a file
• >
– Redirect output, send it to a file
• >>
– Redirect output, append it to a file
Redirecting Outputs
• command > output-file
– Send output of ‘command’ to the file ‘output-file’
instead of the monitor screen
• ls > foo
– Send the directory list to the file named “foo”
• cat foo
– Show contents of “foo”
• date >> foo
– Append the date to the file name “foo”
Cat: Concatenate and Display Files
• The cat utility reads each file in sequence and
writes it on the standard output.
Examples:
• cat myfile
– writes the contents of the file myfile to standard output
• cat doc1 doc2 >doc.all
– concatenates the files doc1 and doc2 and writes the
result to doc.all.
Redirecting Outputs (Contd)
• ls -l > bar
– Send the long directory list to “bar”
• cat foobar
– Display contents of file “foobar”
Redirecting Inputs
• command < input-file
– ‘command’ takes input from ‘input-file’ instead of the
keyboard
• who | sort
– List of users on the system
Pipes Examples (Contd)
• who | wc –l
– How many users are on the system?
– wc display a count of lines, words, and characters in a
file (-l count lines, -w count words, -c count
characters)
• ls –l | more
– Display the long listing page by page
– more browse or page through a text file
Wildcards
• ls foo*
– Lists all files starting with “foo”
• ls *.c
– Lists all files ending with “.c”
• ls f.?
– Lists all files named “f.” plus an extra
character, such as “f.1” or “f.c”
Wildcard Examples (Contd)
• ls a* d* f*
– List all files starting with a, d, f
• ls [adf]*
– same
• ls [a-z]*
– List files starting with any letter
• ls [a-z] *
– List all one letter files, then all files
Sorting Files
• The sort command sorts lines of text files. It reads
information and sorts it alphabetically.
• sort [options] [file-list]
• Options
-b Ignore leading blanks.
-d Sort in dictionary order (Only letters, digits, and blanks are
significant).
-f Ignore the case of words.
-n Sort in numerical order.
-r Reverse the order of the sort.
+n1 [-n2] Specify a field as the sort key, starting with +n1
and ending at –n2 (or end of line if –n2 is not
specified); field numbers start with 0.
Sorting Examples
• ls –s | sort -n
Sorting Questions