0% found this document useful (0 votes)
19 views68 pages

COMP 201 OpenSource L4 ViFileproc

Uploaded by

michellesparks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views68 pages

COMP 201 OpenSource L4 ViFileproc

Uploaded by

michellesparks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

COMP 201 OPEN SOURCE &

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

• Operating system editors let you create


and edit simple text files.
• UNIX includes three editors:
– vi
– emacs
– pico
• We only cover vi in this lecture.
vi Editor

• vi is not user-friendly! You have to


remember all of the commands, in
addition to which mode the editor is in.
• However, vi is very powerful and fast
once you have mastered it.
• The vi editor remains the choice of most
UNIX users.
The view Utility

• The view utility is a read-only version of


vi.
• When you open a file with view, you can
use vi commands, but you cannot
accidentally change the file by saving
your changes.
Two Modes

• Vi works in two modes:


– command mode lets you enter commands that
perform specific vi functions.
– insert mode lets you type text into a file.

• When you first enter the vi editor, it's always in


command mode. Before you can type text in
the file, you must type one of the vi insert
commands, such as i ("insert"), to insert text at
the current cursor location.
Two Modes (Contd)

• Because vi doesn't indicate which mode you're


currently in, distinguishing between command
mode and insert mode is probably the single
greatest cause of confusion among new vi
users.

– Whenever you want to return vi to command mode,


press Esc.
– If you're not sure which mode vi is presently in,
simply press Esc to make sure it's in command
mode and continue from there.
Command Mode

• Most vi commands consist of one or two


letters and an optional number.
• Usually, uppercase and lowercase
versions of commands perform related
but different functions.
– typing a appends the file to the right of
the cursor, while typing A appends the
file at the end of the line.
Command Mode (Contd)

• Most vi commands do not require that


you press Enter to execute them.
• However, commands beginning with a
colon (:) do require that you press Enter
after the command.
– Some books refer to these commands as last-line
mode commands, since when you type the colon
while in command mode, the colon and the
remainder of what is typed appear on the bottom
line of the screen.
Insert Mode

• To type text in a file, type the vi "insert"


command i. This command removes vi from
command mode and puts it into insert mode.
– There are other vi insert commands which are
covered later.

• When you finish typing text, press Esc to


return to command mode. Then you can type
more vi commands.
• If vi seems to act unpredictably, make sure
that you are not in "Caps Lock", which would
cause your entries to be all capital letters.
Starting and Quitting vi

• Starting:
– vi paint begin editing the file paint
– vi you can name the file later
Starting and Quitting vi (Contd)

• Quitting (used only in command mode):


– Saving
:w (when you are entering in long files, please use :w
to save them often)
:w filename (save to filename)
– Saving and Quitting
:wq
Alternatively, type ZZ
– Quitting Without Saving
:q (assumes no changes made)
:q! (discard any changes that were made)
Moving Around in a File

• Although the <PageUp> and <PageDown>


arrow keys work on some systems, the following
commands are guaranteed to work properly on
all systems.
• vi equivalent
– Left Arrow h
– Right Arrow l
– Up Arrow k
– Down Arrow j
Moving Around in a File (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.

– Delete character at cursor x


– Delete character before cursor (backspace) X
– Delete the word starting at the cursor; if the cursor is in
the middle of the word, delete from the cursor to the end
of the word dw
– Delete current line dd
Deleting Text (Contd)
• Note that many of the above commands
can be preceded by a number, for
example:
– 9x delete 9 characters
– 3dd delete 3 lines
– 5dw delete 5 words
Setting Line Number

• Show line numbers :set nu


• Turn off line numbers :set nonu
• display current line number/file name Ctrl-
G
Displaying the current vi 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

• Go to the last line of an open file G


• Return to the first line 1G
• Go to any other line by typing its number
followed by G
– Go to line 12 12G
Copying Text

• The vi command-mode equivalent of “copy and


paste” is yank and put.
– yy or Y (“yank”), p (“put below”), P (“put above”)

• copy current line to "clipboard“ yy


• paste contents of "clipboard" below current line p
• paste contents of "clipboard" above current line P
• copy current line, and next 4 lines to "clipboard“ 5yy
Searching for a Pattern

• You can search forward for a pattern of


characters by typing a forward slash (/),
typing the pattern you are seeking, and then
pressing Enter
Examples:
• search forward (down) for "hello" /hello
• search again, (same direction as original) n
Searching for a Pattern (Contd)
• set case insensitive search :set ic
• set case sensitive search :set noic
• search backward (up) for "hello" ?hello
• search again, (opposite direction as original) N

• Certain special characters ( / & ! . ^ * $ \ ?) have special


significance to the search process and must be
"escaped" when they are used in a search.
– To escape a special character, precede it with a backslash (\).
– To search for the string "anything?" type /anything\? and press
Enter.
Refining the Search
• To match the beginning of a line, start the search string
with a caret (^). For example, to find the next line
beginning with “search", type /^Search

• search for "hello" at start of a line /^hello


• search for "hello" at end of a line /hello$
• search for "hello" or "Hello“ /[hH]ello
• search for "int" as a word (i.e. not print or sprint) /\
<int\>
• search for "eat" but not "beat" or "neat“ /\[^bn]eat
Substituting Text

• The format of the substitute command as it is typed


on the last line is

:[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

• vi makes it convenient to "read" (insert) a file into


the file you are editing.
: line# r filename
– If you do not specify a line number, vi inserts the file at the
current cursor position.

• For example, if you wanted to insert the file


result1at line 24 of the file result2, you would type:
– :24 r result1
– Or you could position the cursor on line 24 and type:
– :r result1
Multiple files

• vi enables you to edit multiple files. For example,


to edit the file result1 while you are editing result2:
1. First, save your current work in result2. Type :w
and press Enter.
2. To edit result1, type :n result1 and press Enter.
3. Make editing changes to result1 and save your
work.
4. When you are finished working with result1 and
have saved your work, you have three choices:
• Exit vi. Type :q and press enter.
• enter to result2. Type :n # and press enter.
• Swap back and forth between two files with the command :n #.
Executing Shell Commands from
Within vi
• :! Shell command
– Allow you execute a shell command
without quitting vi. After executing a shell
command, vi returns to its Command
Mode.
– :! pwd
– :! ls
UNIX File Processing
Objectives

• After studying this lesson, you should be able to


understand and use:
– Redirection
– Pipes
– Wildcards and Regular Expressions
– Commands:
• cat, find, grep, sort, more, wc, head
Standard Files
• All commands perform at least one of the
following operations:
– Input, processing, output
– A typical command performs all three.
• In UNIX three standard files are automatically opened by
the kernel for every command to read input from and
send its output and error messages to. The three
standard files are:
– Standard Input (stdin)
– Standard Output (stdout)
– Standard Error (stderr)
Standard Files (Contd)

• By default
– stdin is associated with keyboard
– stdout is associated with screen
– stderr is associated with screen

• The input ,output and errors of a command can


be redirected to other files by using file
redirection facilities in UNIX.
File Redirection Operators

• <
– 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 foo bar > foobar


– Combine “foo” and “bar” into a new file named
“foobar”

• cat foobar
– Display contents of file “foobar”
Redirecting Inputs
• command < input-file
– ‘command’ takes input from ‘input-file’ instead of the
keyboard

• who > people


– Put the list of users currently on the system into the
file “people”

• sort < people


– Sort by sending “people” as input to sort
Combining Input and Output
Redirection
• command < input-file > output-file
• command > output-file < input-file
– Input to ‘command’ comes from the ‘input-file’ instead of the
keyboard, and the output of the command goes to the ‘output-
file’ instead of the display screen
• sort < people > speople
– Send sorted list to new file “speople”
Unix Pipes (|)
• The pipe operator (|) redirects the output of one
command to the input of another command
• first_command | second_command
– The pipe connects the output of the first command
with the input of the second command
• first_command | second_command |
third_command ...
– The pipe operator can connect several commands on
the same command line
Pipes Examples

• who > people


sort < people
– People acts as a temporary file. Is there a
shortcut? Yes, using pipes.

• 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

* matches any # of characters (0 or


more).
? matches exactly one character.
[ ] and - : character range, a way to specify
a sub range of characters to match.
Wildcard Examples

• 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

• By default, the ls command sorts the files in a


case-sensitive manner. It first lists files that
begin with uppercase letters and then lists those
that begin with lowercase letters.
• To force ls to list output one file per line, you can
use the -1 option (that’s number one, not a
lowercase L).
• To sort filenames alphabetically regardless of
case, you can use sort –f.
Sorting Examples

• ls -1F ls -1F | sort -f


Sorting Examples (Contd)

• Of the files in your home directory, which are the


largest? The ls –s command indicate the size of
each file, in blocks, and sort –n sorts
numerically:

• ls –s | sort -n
Sorting Questions

• Q9: It would be more convenient if the largest


files were listed first in the output. What
command will have the following output?
Sorting Examples (Contd)

• Instead of listing all the files, use the head


command, and specify that your want to see
only the top five entries.
• head display first few lines of files

• ls –s | sort –nr | head -5


Finding Files

• The find command can be used to find whether


a particular file exists in your file structure.
• find path… expression
– path A path of a starting point in the directory
hierachy.
• Although Linux does not use it, other UNIX versions
require the -print option after the filename to display the
names of fields the find command locates.
Finding Files (Contd)

• Useful expressions for the find command:


-exec commandExecute command.
-ctime n True if the file was created n days ago.
-mtime n True if the file was modified n days ago.
-name pattern True if the filename matches pattern.
-print Print names of the files matched.
-type c True if the files if of type c
(d Directory, f File, l Link)
-user uname True if the file is owned by user uname.
Finding Examples
• To find just those files that are C source files (those that
have a .c suffix) in your current working directory.
find . –name ‘*.c’ –print

• To find just those files that have been modified in the


past seven days.
find . –mtime -7 –name ‘*.c’ –print
find . –mtime 7 –name ‘*.c’ –print will match only those
files that were modified exactly seven days ago.
Finding Examples (Contd)
• To find a list of directories in your current
working directory:
find . –type d –print
• To find more information about each of the
above directories, you can use –exec option:
find . –type d –exec ls –ld {} \;
The –exec option must be used with { }, which will be
replaced by the matched filename, and \; at the end of
the command. And there is a space between the {} and
the \; characters.
Questions

• Q10: Look across the /dev and /sbin


directories for filenames that contain the
pattern cp.

• Q11: Find those C source files that haven’t


been modified for at least 30 days.
Questions
• The find command is commonly used to remove core
files that are more than a few days old. These core files
are copies of the actual memory image of a running
program when the program dies unexpectedly.
• Q12: Find all files called core that have a creation time
that’s more than four days ago.

• Q13: Remove the core files.


grep

• You can use find to search for files, and


you can use grep to search within files.
• grep search the files for a pattern
grep [options] pattern [files]
• grep means global regular expression
print.
grep options

• The most helpful grep options:


-c List a count of matching lines only
-i Ignore the case of the letters in the
pattern.
-l List only the names of files that
contain the specified pattern.
-n Include line numbers.
grep examples

• Find all the lines that contain a string


matching the expression “foo” in the file
“file”:
grep foo file
• Find all the lines that contains “cse251” in
the password file:
grep cse251 /etc/passwd
grep examples (Contd)

• To number the output lines:


grep -n cse251 /etc/passwd
• Suppse ~/State directory contains one file
for every state in the US, and each file
contains the name of all the cities in the
state. To find the names of states that
have a city called Portland:
grep -l Portland ~/States
Regular Expressions

^ Beginning of the line


$ End of the line
. Any single character
[ ] Matches any character enclosed in
brackets.
[^] Matches any character not enclosed
in brackets.
* Repetition (0 or more times)

You might also like