0% found this document useful (0 votes)
16 views37 pages

17CS35 - Module - 3 Vi Editor and Shell VTUPulse.com

Module 3 covers the vi editor, detailing its three modes: command mode, input mode, and ex mode, along with commands for editing text. It explains how to navigate between modes, perform text insertion, replacement, and saving files. Additionally, it includes examples and commands for using the shell and regular expressions.

Uploaded by

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

17CS35 - Module - 3 Vi Editor and Shell VTUPulse.com

Module 3 covers the vi editor, detailing its three modes: command mode, input mode, and ex mode, along with commands for editing text. It explains how to navigate between modes, perform text insertion, replacement, and saving files. Additionally, it includes examples and commands for using the shell and regular expressions.

Uploaded by

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

Module 3 Unix and

Shell Programming

MODULE : 3
The vi editor. Basics. The .exrc file. Different ways of invoking and quitting vi. Different modes
of vi. Input mode commands. Command mode commands. The ex mode commands. Illustrative
examples Navigation commands. Repeat command. Pattern searching. The search and replace
command. The set, map and abbr commands. Simple examples using these commands.
The shells interpretive cycle. Wild cards and file name generation. Removing the special
meanings of wild cards. Three standard files and redirection. Connecting commands: Pipe.
Splitting the output: tee. Command substitution. Basic and Extended regular expressions. The
grep, egrep. Typical examples involving different regular expressions.
Topics from chapters 7, 8 and 13 of text book 1. Topics from chapter 2 and 9 ,10 of
text book 2

THE VI EDITOR

vi BASICS

Invoking vi editor

vi <filename>

 In all probability, the file doesn’t exist, and vi presents you a full screen with the filename
shown at the bottom with the qualifier, [New file].
 The cursor is positioned at the top and all remaining lines of the screen show a ~. They
are non-existent lines.
 The last line is reserved for commands that you can enter to act on text. This line is also
used by the system to display messages.
 When file is opened in a vi editor, by default it will be opening in a command mode.
This is the mode where you can pass commands to act on text, using most of the keys
of the keyboard.
 This is the default mode of the editor where every key pressed is interpreted as a
command to run on text. You will have to be in this mode to copy and delete text.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 1


Module 3 Unix and
Shell Programming

 To enter text, you must switch to the input mode. First press the key i, and moved to
input mode and ready to input text. Subsequent key depressions will then show up on the
screen as text input.
 After text entry is complete, the cursor is positioned on the last character of the last line.
This is known as current line and the character where the cursor is stationed is the
current cursor position. This mode is used to handle files and perform substitution.

Sample vi editor file with some text entered


This is the vi editor [Enter]
It operates in three different modes
This is just a sample text.
~
~
~

 Use [Esc] key to revert to command mode.


 Actually, the text entered has not been saved on disk but exists in some temporary storage
called a buffer.
 To save the entered text, you must switch to the execute mode
 Invoke the execute mode from the command mode by entering a : which shows up in The
last line.

THE THREE MODES OF vi EDITOR

Command Mode:

 When user starts up vi editor, user are in “command mode”. The default mode of the
editor where every key pressed is interpreted as a command to run on text.
 This mode allows to copy, delete text (edit files) and to move to any other modes. When
keys are pressed in this mode it simply performs functions, it doesn’t displayed on the
screen. When you are in command mode, letters of the keyboard will be interpreted as
commands.
 Some of the command mode commands are : To delete a text command x is used, to
delete entire line use dd command, to put the character use P or p command, to yank the

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 2


Module 3 Unix and
Shell Programming

text use y command, use of yy copies entire line, to undo the recent actions use u
command.
 When user in a command mode, the letters of the keyboard will be interpreted as
commands. These commands are one or two characters long, and can be entered with
few keystrokes.
 Once the editing is finished save the changes by moving to ex mode by using :w
command.
 Unnecessary pressing of [Esc] in this mode sounds a beep but also conforms you are in
this mode.
 As shown in the figure, from command mode user can get into the input mode by
giving any of these keys i a I A o O r R s S.
 The control can be brought back to the command mode by using the <Esc> key.
 From within the command mode the user can get into the ex mod by using the :
Input mode:
 To enter the text, have to change from default command mode to Input mode
 Every key pressed after switching to this mode actually shows up as text. This mode is
invoked by pressing one of the keys among i a I A o O r R s S.
 In the above command list i and I stands for insertion, a and A stands for appending the
text, o and O stands for opening a new blank new line, r and R stands for replacement of
text, s and S stands for substitution of new text. This mode permits the insertion of new
text, appending the existing text and replacement, substitution of text.
 The i can be used to insert text anywhere in a line, I insert text only at the beginning of a
line.
 To append text to the right of the cursor position use a. To append text at the end of the
line use A.
 To open a line below the current line use o. To open a line above the current line use O.
 To replace one single character with another, user r followed by the character that
replaces the one under the cursor.
 To replace more than a single character use R followed by the text and then press [Esc].
 To replace a single character with multi-character text, move the cursor to the point and
then press s.
 The S replaces the entire line irrespective of the cursor position. After pressing S, the
entire line vanishes from sight. Key in your text, and then press [Esc].
 As shown in the figure, at any time user can go back to the command mode from insert
mode by pressing the <Esc> key.
Ex mode (Last line mode):

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 3


Module 3 Unix and
Shell Programming

 This mode is also known as the last-line mode and allows the user to use the command
in the bottom line in vi screen.
 This mode is used to handle files (like saving) and perform substitution.
 Pressing a : in the command mode invokes this mode and cursor moves to the bottom of
the screen.
 The bottom line of the vi screen is called the command line and it’s used to display the
entered ex mode commands. The commands entered in this mode are displayed in the
command line, the last line. Anything entered in front of the colon : prompt it is taken as
an ex command.
 Some of the sample ex mode commands are : :w to save the file contents, command
:wq used to save and quit the editor, :q to just quit the editor, :q! to quit the editor by
discarding the changes made
 After inserting ex mode command press [Enter], so it moved back to command mode.
After the command is run then it’s moved back to the default command mode.

 Switching between the modes is depicted in the following diagram.

 As shown in the figure; when user starts up vi editor, user is in “command mode”. From
command mode user can enter to input mode by pressing I,i,a,A,s,S,R,r,O,o commands.
From input mode user can move back to command mode by pressing [esc]. From
command mode to ex mode is invoked by pressing :. From ex mode pressing [enter]
move to the command mode.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 4


Module 3 Unix and
Shell Programming

*****************Review Questions/ University Questions*************************


Explain the different modes of vi editor with neat diagram.
Explain the three modes of vi and explain how you can switch from one
mode to another.
*******************************************************************************

The Repeat Factor


vi provides repeat factor in command and input mode commands. The repeat factor used as a
command prefix to repeat the command as many times as the prefix. Command mode command
k moves the cursor one line up. 10k moves cursor 10 lines up, here 10 is acting as a repeat factor.
So it helps to speed up the operation.

INPUT MODE – ENTERING AND REPLACING TEXT


To enter from command mode to input mode following commands are used:
 Insert and Append (I,a,I and A)
 Replace (r, R, s and S)
 Open a line( o AND o)
Note: After the completion of text entry using any of these commands, have to return back to the
command mode by pressing [Esc].

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 5


Module 3 Unix and
Shell Programming

Insertion of Text (i and a)


The simplest type of input is insertion of text.
i Existing text will be shifted right
Pressing i key changes the mode from command to input. Further key depression (press) will
result in text being entered and displayed on the screen.
If the i command is invoked with the cursor positioned on existing text, text on its right will be
shifted further without being overwritten.
The insertion of text with ‘i’ is shown in the fig. along with the position of the cursor.
The vi editor
i full-screen [Esc]
the vi full-screen editor
To append text to the right of the cursor position, use ‘a’
a Existing text will also be shifted right
followed by the text wish to key in. After finished editing, press [Esc].
the vi editor
a, is very useful [Esc]
the vi editor, is very useful

Insertion of Text at line Extremes (I and A)


I and A work at line extremes by performing necessary navigation to move there
I Inserts text at beginning of line
A Appends text at end of line
These two commands are very useful to enter comment lines in C programs
Use I on an existing line that need to convert to a comment, and then enter the symbol /* After
pressing [Esc], use A to append */ at the end of the line and press [Esc] again.
Using I and A to create a comment line in a C program.
The following code is used to check the palindrome numbers
I/* [Esc]
/* The following code is used to check the palindrome numbers
*/ [Esc]
/* The following code is used to check the palindrome numbers */

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 6


Module 3 Unix and
Shell Programming

Opening a new line (o and O)


To open a line below from anywhere in a line, simply press ‘o’. This inserts an empty line below
the current line.
‘O’ (Uppercase O) opens a line above the current line. After completion of text entry press [Esc].
Vi and ex are one and the same editors
o It is due to William Joy[Esc]
Vi and ex are one and the same editors
It is due to William Joy

Replacing Text (r,s,R and S)


To change existing text, vi provides mainly four commands.
To replace a single character with another, use ‘r’
r Used to replace a single character
Vi and ex are one and the same editors
rX
Vi and eX are one and the same editor
So it replaces the text under the cursor by the new text entered.
When more than one character is need to be replaced with multiple characters ie (for example a
character b with csb, here one character need to replace with three.) use a ‘s’.
s Replaces one character with many
vi deletes the character under the cursor and switches to Input Mode. It may also show $ at that
location to indicate that replacement will not affect text on its right.
To replace multiple characters, use a repeat factor. 3s replaces three characters with new text.
Vi is a link of ex
s one[Esc]
vi is one link of ex
R and S command act similar to lowercase ‘r’ and‘s’ except that act on a larger group of
characters.
R Replaces all text on the right of the cursor position
S Replaces the entire line irrespective of cursor position (Existing line disappears)

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 7


Module 3 Unix and
Shell Programming

vi is one link of ex, just an example text line


R simple[Esc]
vi is one link of ex, just an simple text line

Summary of Input mode commands

Command Function
i Inserts text before the current character
I Inserts text at the beginning of current line
a Appends text after the current character
A Appends text at the end of the current line
o Opens a line under the current line and inserts supplied text in it.
O Opens a line above the current line and inserts supplied text in it.
r Replaces the current character at the cursor with the new one
R Replaces a set of characters with a new set of characters
s Substitute the current character with the supplied text
S Substitute the current line with the supplied text

OR
Command Function
Insert text to the left of cursor (existing text shifted right)
i
Append text to right of cursor (existing text shifted right)
a
Insert text at beginning of line (existing text shifted right)
I
Append text at the end of line
A
Opens a line below
o
Opens a line above
O
Replaces single character under cursor with ch
rch
Replaces text from cursor to right(existing text overwritten)
R
Replaces single character under cursor with any number of character
s

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 8


Module 3 Unix and
Shell Programming

Replaces entire line


S
SAVING TEXT AND QUITTING – THE EX MODE
When you edit a file using vi, the original file is not distributed as such, but only a copy of it that
is placed in a buffer. From time to time, you should save your work by writing the buffer
contents to disk to keep the disk file current. When we talk of saving a file, we actually mean
saving this buffer. You may also need to quit vi after or without saving the buffer.
Some of the save and exit commands of the ex mode is:

Saving Your work (:w)


 To save the buffer and remain in the editor use :w command. This command will help to
write the buffer to disk.
 Enter a :, which appears on the last line of the screen, then w and finally [Enter]:
 :w[Enter]
 “samplefile”, 9 lines, 321 characters
 With the :w command optionally it is possible to specify a filename as well. In that case,
the contents are separately written to another file.
Saving and Quitting (:x and :wq)
 To save the contents and quit the editor (i.e., return to the shell), use the :x (exit)
command.
 : x [Enter]
 “samplefile”, 9 lines, 321 characters
 $_
 To save and quit use :wq command.
 Note: It is also possible to save and quit the editor is by using ZZ, a command mode
command.

Aborting Editing( :q)


 To abort the editing process and quit the editing mode without saving the buffer
use :q command.
:q[Enter] Won’t work if buffer is unsaved
$_
 Vi also has a safety mechanism that prevents from aborting accidentally if the file is
modified. The following message is typical when try to do so:

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 9


Module 3 Unix and
Shell Programming

 No write since last change (:quit! Overrides)


 If the buffer has changed and want to abandon (reject) the changes, then use,
:q! Ignores all the changes made and quits
 It returns to the prompt irrespective of the status of the buffer.
Writing selected lines
 w can be prefixed by one or two addresses separated by a comma to save lines to a
different file. The command
 :10, 50w newpgm.c Writes 41 lines to newpgm.c file
 Here saves lines 10 through 50 to the file newpgm2.c.
 To save a single line, command is:
 :6w newpgm.c Writes 6th line to another file
 The symbols ‘.’ And ‘$’ have special significance- the (dot) represents the current line
and $ represents the last line of the file. It can be used singly or in combination.
 Example:
 :.w tempfile Saves current line(where cursor is positioned)
 :$w tempfile Saves last line
 :.,$w tempfile Saves current line through end

Escape to the UNIX shell (:sh and [Ctrl-z])


 To obtain the shell prompt use ex mode command :sh command.
 This returns a shell prompt. It allows executing any UNIX commands and then returning
to the editor using [ctrl-d] or exit.

Recovering from a crash (: recover and –r)


 The vi stores most of its buffer information in a hidden swap file. This file will remain on
disk. To save the file contents use either :recover or vi –r foo to recover as much of foo as
possible.

Save and Exit Commands of the ex Mode


Command Action
:w Saves file and remains in editing mode
:x Saves file and quits editing mode
:wq Saves file and quits editing mode
:w pgm.c Like save As , it saves the file contents to new file called pgm.c
:w! pgm.c As above, but overwrites existing file.
:q Quits editing mode when no changes are made to a file.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 10


Module 3 Unix and
Shell Programming

:q! Quits editing mode but after abandoning changes


:n1,n2w module Writes lines n1 to n2 to file module
:.w file.c Writes current line to the file build.sql
:$w db.sql Writes last line to the file build.sql
:!cmd Runs cmd command and returns to Command Mode
:sh Escapes to UNIX shell
:recover Recovers the file from a crash

NAVIGATION
Functions of different commands in command mode are described in this section. A command
mode command doesn’t show up on the screen but simply performs the function.
Movement in the Four Direction (h,j,k and l)
Vi provides the keys h,j,k and l to move the cursor in the four directions. These keys are placed
adjacent to one another in the middle row of the keyboard. Without a repeat factor, these keys
move the cursor by one position.
Use these keys for moving the cursor vertically:
k Moves the cursor UP
j Moves cursor DOWN
To move the cursor along a line, use these commands:
h Moves cursor LEFT
l Moves cursor RIGHT
The repeat factor can be used as a command prefix with all these four commands.
4k moves the cursor 4 lines up and 20h takes it 20 characters to the left.
Summary Table

Command Meaning Direction

k moves cursor up UP

j Moves cursor down DOWN

h Moves the cursor left LEFT


l
Moves the cursor right RIGHT

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 11


Module 3 Unix and
Shell Programming

Refer class notes for example on scrolling diagram on page no 132 in book

Word Navigation (b,e and w)


There are 3 basic navigation commands:
b Moves back to beginning of word
e Moves forward to end of word
w Moves forward to beginning of word
A repeat factor speeds up cursor movement along a line.
Example:
5b takes the cursor five word back, while 3w takes the cursor three words forward. A word is
simply a string of alphanumeric characters and _.
The keys B,E and W perform functions similar to those of their lowercase counterparts except
that punctuation is skipped.

Moving to Line Extremes

Moving to the beginning or end of a line is a common requirement. It is achieved by keys 0,| and
$.
To move to the first character of a line use 0 or |
0 or | 30| moves cursor to column 30
The | takes a repeat factor and using that, it is possible to position the cursor on a certain column.
To position the cursor on column 30, use 30|.
$ moves to the end of the current line
$ Moved to the end of the current line
The use of these commands along with b, e, and w is allowed

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 12


Module 3 Unix and
Shell Programming

Refer class notes for example on scrolling diagram on page no 133 in book

Scrolling

Faster movement can be achieved by scrolling text in the window using the control keys. The
two commands for scrolling a page at a time are
ctrl-f scrolls forward
ctrl-b scrolls backward
10ctrl-f scroll 10 pages and navigate faster
ctrl-d scrolls half page forward
ctrl-u scrolls half page backward
5ctrl-d scrolls 5 pages forward.
Absolute Movement
To know the current line number can press [Ctrl-g]:
“/etc/passwd” [Read Only] line 45 of 125 ---45%---
The cursor is on line 45 (45% of 125), and this read only file has 125 lines in all.
Use G command with line number as repeat factor to locate the offending lines.
To move to the 40th line, use
40G Goes to line number 40
To move to the beginning of the file, use
1G Goes to line number 1
The end of the file is reached by simply using
G Goes to end of file
Summary Table
Command Function
Moves back to the beginning of a word
b

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 13


Module 3 Unix and
Shell Programming

e Moves forward to the end of a word

w Moves forward to the beginning of next word

0 (zero) Moving to the beginning of a line

| Moving to the certain column of a line

$ Moving to the end of a line

ctrl – f Scrolls one page forward = > 2 ctrl – f

ctrl – b Scrolls one page backward

ctrl - d Scrolls half page forward

ctrl – u Scrolls half page backward

20G Moves the cursor to the beginning of the line number 20

1G Moves the cursor to the beginning of the very first line

G Moves the cursor to the beginning of the last line

EDITING TEXT
Deleting text ( x and dd )
The simplest text deletion is achieved with the x command. This command deletes the character
under the cursor.
Move the cursor to the character that needs to be deleted and then press x,
The character under the cursor gets deleted the text on the right shifts left to fill up the space.
Repeat factor also applies here. 4x deletes the current character as well as three characters from
the right.
Deletion of the left character can be handle by the “ X ” command .
Entire lines can be deleted(removed) with “ dd “ command. Here also repeat factor applies.10dd
deletes the current line and 9 lines below it.
This is the vi full-screen editor from UCB
x

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 14


Module 3 Unix and
Shell Programming

This is the vi fullscreen editor from UCB


Four spaces back
This is the vi full-screen editor from UCB
4x
This is the vi screen editor from UCB

This is the vi editor from UCB


It is superior to ed, and friendlier too
It is due to William Joy
It is slow in getting started but is quite powerful
dd
It is superior to ed, and friendlier too
It is due to William Joy
It is slow in getting started but is quite powerful
2dd
It is slow in getting started but is quite powerful

Moving text ( p and P )


Vi uses these 2 commands for all “ put “ operations that follow delete or copy operations.
p => it places the character right to the cursor . Puts the deleted or yanked text after the
current character.
P => it places the character left to the cursor. Puts the deleted or yanked text before the
current character.
Example : To correct sdtio.h to stdio.h, have to transpose the character d to t. Delete the d and
put it after the t. Move the cursor to the d in sdtio.h and use these two commands.
x sdtio.h becomes stio.h – cursor on t
p d put on right- stio.h becomes stdio.h
P places text on the left of the cursor.
To put entire line at different location, p places text below the current line and P places text
above.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 15


Module 3 Unix and
Shell Programming

Coping text(y,p)
Vi uses the term yanking for copying the text. In vi extracting a copy of the required text is
known as yanking.
y => it copies the single character
yy => it copies the entire line – copies the current line
10yy=> it copies the current line & below 9 line.
This copied text has to be placed at the new location. The put commands are the same – p and P.
Joining Lines (J)
To loin the current line and the line following it, use J. This command will join the 2 lines by
deleting the newline character which is present between the two lines
4J 4J joins following 3 lines with current one

Undoing last editing instructions


Last change can be made undo by pressing u. This will undo the most recent single editing
changes by restoring the position before the change. It undoes only the last edit, must use in
command mode
When a number changes have been made to a single line, vi allows user to discard all changes
before user move away from the current line. It undoes all the changes in the current line.
U don’t move away from current line.
The command reverses all changes made to the current line i.e all modifications that have been
made since the cursor was moved to this line.

Summary Table
Command Meaning
p Puts the deleted(or yanked) text after the current character
P Puts the deleted(or yanked) text before the current character
x Deletes the current character
y Yanks a text object.
X Deletes the character before the cursor
d Deletes a line or a range of lines
u Undoes only the last edit

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 16


Module 3 Unix and
Shell Programming

U Undoes all the changes on the current line

REPEATING THE LAST COMMAND (.)


The . (dot) command is used for repeating both Input and Command Mode commands that
perform editing task. ‘Use the actual command only once, and then repeat it at other places with
the . dot command.
Ex: Deleted two lines of text with 2dd, then to repeat this operation elsewhere, have to do is to
position the cursor at the desired location and press . , this will repeat the last editing instruction
performed.
Ex: Indent a group of lines use : i[Tab][esc], only once . Then you can move the cursor to each
line and simply press .
The . Command can be used to repeat only the most recent editing operation- insertion, deletion
or any action modifies buffer
Doesn't applicable to navigation, and searching options. Search commands (/ and ? ) cannot be
repeated using the (.) command as these commands do not make changes to the editor buffer.

SEARCHING FOR A PATTERN (/ AND ? )


Searching can be made in both forward and reverse directions and can be repeated. It is
initiated from the command mode by pressing /, which is shows up in the last line.
The format is as follows: /[pattern] [Enter]
Ex: /printf[Enter] Searches forward
The search begins forward to position the cursor on the first instance of the word (printf). Vi
searches entire file, so if the pattern can’t be located until the end of file , the search wraps
around to resume from the beginning of the file. i.e during the forward search after the end of
the file is reached, the search continues from the beginning of the file. If the search still fails, vi
responds with the message Pattern not found

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 17


Module 3 Unix and
Shell Programming

? Pattern [Enter]
The sequence searches backward for the most previous instance of the pattern, the wraparound
features also applies here but in the reverse direction. i.e after the beginning of the file is
reached the search continues from the end of the file. Both of these command are command
mode commands.
Repeating the last pattern search ( n and N)
To repeat a search in the direction the previous search was made with /and ? use n.
n Repeats search in same direction of original search
n => the user can press “ n ” repeatedly to scan all instances of the string. Cursor is positioned at
the beginning of the pattern
N => it reverses the direction pursued by n , i.e. this is used to retrace the search path of n
Summary table

Command Function
/pat Searches forward for the pattern

?pat Searches backward for the pattern

n Repeat search in same direction along which previous search was made

N Repeat search in REVERSE direction along which previous search was made

SUBSTITUTION --- SEARCH & REPLACE (:S)


Vi offers another powerful feature that of substitution, which is achieved with the ex mode s
(substitution) command.
The syntax is as follows
:address/ source_pattern / target_pattern/flags

Here the source_pattern is replaced with target_pattern in all lines specified by address.
Address can be one or a pair of numbers. 1,$ -> addresses all lines in a file i.e. from 1 st line to
last line. Common flag is “ g “ , which carries out the substitution for all occurrences of the
pattern in line.

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 18


Module 3 Unix and
Shell Programming

Example: :1,$s/csa/csb/g Searches for csa and its substituted by csb in entire file

If we omit the “ g” flag , the substitution will be carried out only for the first occurrence in each
addressed lines.
The target pattern is optional. If it is not mentioned then it will delete all instances of the source
pattern in all lines matched by the address.
Example: 1,50 s/ note / /g deletes note everywhere in line 1 to 50
The range of lines that are to be affected by the substitution can also be specified.

:3,10 s / director / member / g Substitute line 3 through 10


: .s / director / member / g Only the current line
: $s / director / member / g Only the current line
: %s/printf/scanf/g Entire file printf is substituted by scanf
Interactive Substitution
To selectively replace a string, use confirmatory(c) parameter as a flag.
Example: :1,$s/director/member/gc
Each line is selected in turn, followed by a sequence of carets in the next line, just below the
pattern that requires substitution. The cursor is positioned at the end of this caret sequence,
waiting for your response. This sequence repeated for each of the matched lines. A y performs
the substitution, any other response doesn't
:1,$s/printf/scanf/gc
printf(“string is %s”, a);
When the above command is executed, the vi pauses at the replacement point and waits for the
user’s response. A yes(y or Y) make the replacement whereas no answer (n or N) doesn’t make
the replacement and the search continues.

THE FILE .EXRC


When we open a file using vi, we do some settings depending on our needs say, to set line
number, to set indentation, to set the tab space etc in the file. However, these settings or
customizations last only till the file is open. Some of these settings any user would always like to
retain on opening any file. These settings can be grouped in a file called .exrc file, and hence the

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 19


Module 3 Unix and
Shell Programming

settings become permanent. Whenever a file is opened using vi, vi looks for the .exrc file in the
home directory of the user. If present, it reads the file and applies the settings in the file being
opened. And hence any customization we would like to have in the file should be put in this.
.exrc is a file located in $HOME/ .exrc, which holds many ex mode commands and a
series of set commands. Execution of these provides a suitable working environment with the vi.
The vi reads file $HOME/.exrc on startup. This file helps to create abbreviations, redefine the
keys to behave differently and also make variable settings.
Typical entries of a .exrc file is depicted as
$ cat.exrc
set number
ab p printf
ab s scanf
map x :wq^M
When file is opened in vi editor, the .exrc file in the home directory will be read and all the
commands present in it will be executed, thus building up the required editing environment.

THE SET, MAP AND ABBR COMMAND

The set command


The several options can be used by the user to customize the vi environment. For example, line
number can be made to appear automatically, the current mode of the vi editor can be made to be
displayed automatically, case sensitivity can be removed during pattern matching. For this an ex
mode command called the set command is used. By using set command it is possible to change
the look and feel of vi .
:set number command sets the line number option. When this option is set, line numbers appear
automatically. By default, no line number appears in vi editor. To remove the line number can
use : set nonumber
The command :set showmode is used to display the mode in which the editor is present
currently. Automatic displaying of the modes can be stopped by using noshowmode option along
with the set command i.e. :set noshowmode

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 20


Module 3 Unix and
Shell Programming

The command :set autoindent automatically indents every new line keyed in by the user with
one or several tabs. Auto indenting can be cancelled by using the command :set noautoindent
Options that are set using set commands are applicable only to those sessions in which they are
given. If the user wants the settings to be applicable permanently, relevant settings must be set
using corresponding commands in the .exrc file.
Command Meaning

:set ic Ignores case when searching

:set ai Sets autoindent

:set noai To unset autoindent.

:set number Displays lines with line numbers on the left side.

:set ro Changes file type to "read only"

Map Command
Assign existing vi commands to a custom key or define own custom commands. Using this
command one can connect one or more commands to a single key. This process is known as
mapping. Such a facility allows the user to perform complex editing tasks with a single
keystroke.
For example: map Q :q!
This command maps the Q key with the quit operation. So, whenever the user wants to quit the
file, instead of typing ':q!', the user can simply type 'Q' from the escape mode.

The ab command
This is also an ex mode command and is used to have short hand abbreviations for commonly
used expressions or the input text.
For example while writing the C programs, the printf and scanf words can be abbreviated using
the single characters p and s respectively as shown below.
:ab p printf
:ab s scanf

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 21


Module 3 Unix and
Shell Programming

When the abbreviated character p followed by a space character or the enter key is used the text
printf appears at the place of p.
So ab command helps to define abbreviations and that are expanded into a specific sequence of
characters when they are encountered while users are typing text in Insert mode. Frequently used
words can be abbreviated. For example, say, a user uses the word 'include' lot many times. The
user can create an abbreviation 'inc' for the word include.
ab inc include
Whenever user type 'inc' followed by a space in a file, it automatically gets converted to 'include'.
While abbreviations set during a session are valid only for that session If the user wants the
abbreviations to be applicable permanently then corresponding abbreviations entries must be
made in .exrc file.
********************Review Questions: University Questions ******************
Explain what the following commands do i) :.,10w foo ii) :$w! foo. In
which mode are the commands executed.
How do commands u and U differ? When will U fail to work?
Explain the ex and input mode vi editor commands with example
With example explain the different navigation commands
Illustrate how to search and replace a pattern with example

Prepared By: Sharanya P S, CSE Dept, VCET PUTTUR Page 22


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

THE SHELL
Introduction
Shell acts as both a command interpreter as well as a programming facility.

The shell and its interpretive cycle


The shell sits between you and the operating system, acting as a command interpreter. It reads
your terminal input and translates the commands into actions taken by the system. The shell is
analogous to command.com in DOS. When you log into the system you are given a default shell.
When the shell starts up it reads its startup files and may set environment variables, command
search paths, and command aliases, and executes any commands specified in these files. The
original shell was the Bourne shell, sh. Every Unix platform will either have the Bourne shell, or a
Bourne compatible shell available.

Numerous other shells are available. Some of the more well known of these may be on your Unix
system: the Korn shell, ksh, by David Korn, C shell, csh, by Bill Joy and the Bourne Again SHell,
bash, from the Free Software Foundations GNU project, both based on sh, the T-C shell, tcsh, and
the extended C shell, cshe, both based on csh.

Even though the shell appears not to be doing anything meaningful when there is no activity at the
terminal, it swings into action the moment you key in something.

The following activities are typically performed by the shell in its interpretive cycle:

 The shell issues the prompt and waits for you to enter a command.
 After a command is entered, the shell scans the command line for meta characters and
expands abbreviations (like the * in rm *) to recreate a simplified command line.
 It then passes on the command line to the kernel for execution.
 The shell waits for the command to complete and normally can’t do any work while the
command is running.
 After the command execution is complete, the prompt reappears and the shell returns to
its waiting role to start the next cycle. You are free to enter another command.

Pattern Matching – The Wild-Cards


A pattern is framed using ordinary characters and a meta character (like *) using well- defined
rules. The pattern can then be used as an argument to the command, and the shell will expand it
suitably before the command is executed.

The meta characters that are used to construct the generalized pattern for matching filenames
belong to a category called wild-cards. The following table lists them:

Wild-card Matches
* Any number of characters including none

Dept. of Computer Science and Engineering 1


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

? A single character
[ijk] A single character – either an i, j or k
[x-z] A single character that is within the ASCII range of characters x and z
[!ijk] A single character that is not an i, j or k (Not in C shell)
[!x-z] A single character that is not within the ASCII range of the characters x and z (Not in C
Shell)
{pat1,pat2...} Pat1, pat2, etc. (Not in Bourne shell)

Examples:

To list all files that begin with chap, use, $ls chap*

To list all files whose filenames are six character long and start with chap, use , $ls chap??

Note: Both * and ? operate with some restrictions. for example, the * doesn’t match all files
beginning with a . (dot) or the / of a pathname. If you wish to list all hidden filenames in your
directory having at least three characters after the dot, the dot must be matched explicitly.

$ ls .???*

However, if the filename contains a dot anywhere but at the beginning, it need not be matched
explicitly.

Similarly, these characters don’t match the / in a pathname. So, you cannot use, $cd /usr?local to
change to /usr/local.

The character class


You can frame more restrictive patterns with the character class. The character class comprises a
set of characters enclosed by the rectangular brackets, [ and ], but it matches a single character in
the class. The pattern [abd] is character class, and it matches a single character – an a,b or d.

Examples:

$ls chap0[124] - Matches chap01, chap02, chap04 and lists if found.

$ls chap[x-z] - Matches chapx, chapy, chapz and lists if found.

Dept. of Computer Science and Engineering 2


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

You can negate a character class to reverse matching criteria. For example,

- To match all filenames with a single-character extension but not the .c ot .o files, use *.[!co]

- To match all filenames that don’t begin with an alphabetic character, use [!a-zA-Z]*

Escaping and Quoting


Escaping is providing a \ (backslash) before the wild-card to remove (escape) its special meaning.

For instance, if we have a file whose filename is chap* (Remember a file in UNIX can be names
with virtually any character except the / and null), to remove the file, it is dangerous to give
command as rm chap*, as it will remove all files beginning with chap. Hence to suppress the
special meaning of *, use the command rm chap\*

To list the contents of the file chap0[1-3], use ,

$cat chap0\[1-3\]

A filename can contain a whitespace character also. Hence to remove a file named My
Documend.doc, which has a space embedded, a similar reasoning should be followed:

$rm My\ Document.doc

Quoting is enclosing the wild-card, or even the entire pattern, within quotes. Anything within
these quotes (barring a few exceptions) are left alone by the shell and not interpreted. When a
command argument is enclosed in quotes, the meanings of all enclosed special characters are
turned off.

Examples:

$rm „chap*‟ Removes files chap*

$rm “My Document.doc” Removes file My Document.doc

Redirection : The three standard files


The shell associates three files with the terminal – two for display and one for the keyboard.
These files are streams of characters which many commands see as input and output. When a user
logs in, the shell makes available three files representing three streams. Each stream is associated
with a default device: -

Dept. of Computer Science and Engineering 3


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

Standard input: The file (stream) representing input, connected to the keyboard.

Standard output: The file (stream) representing output, connected to the display.

Standard error: The file (stream) representing error messages that emanate from the
command or shell, connected to the display.

The standard input can represent three input sources:

The keyboard, the default source.

A file using redirection with the < symbol.

Another program using a pipeline.

The standard output can represent three possible destinations:

The terminal, the default destination.

A file using the redirection symbols > and >>.

As input to another program using a pipeline.

A file is opened by referring to its pathname, but subsequent read and write operations identify
the file by a unique number called a file descriptor. The kernel maintains a table of file descriptors
for every process running in the system. The first three slots are generally allocated to the three
standard streams as,

0 – Standard input

1 – Standard output

2 – Standard error

These descriptors are implicitly prefixed to the redirection symbols.

Examples:

Assuming file2 doesn’t exist, the following command redirects the standard output to file
myOutput and the standard error to file myError.

$ls –l file1 file2 1>myOutput 2>myError

Dept. of Computer Science and Engineering 4


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

Filters: Using both standard input and standard output


UNIX commands can be grouped into four categories viz.,

1. Directory-oriented commands like mkdir, rmdir and cd, and basic file handling
commands like cp, mv and rm use neither standard input nor standard output.
2. Commands like ls, pwd, who etc. don’t read standard input but they write to standard
output.
3. Commands like lp that read standard input but don’t write to standard output.
4. Commands like cat, wc, cmp etc. that use both standard input and standard output.

Commands in the fourth category are called filters. Note that filters can also read directly from
files whose names are provided as arguments.

Example:

To perform arithmetic calculations that are specified as expressions in input file calc.txt and
redirect the output to a file result.txt, use

$bc < calc.txt > result.txt6.

Pipes: Connecting Commands


With piping, the output of a command can be used as input (piped) to a subsequent command.

$ command1 | command2

Output from command1 is piped into input for command2.

This is equivalent to, but more efficient than:

$ command1 > temp

$ command2 < temp

$ rm temp

Examples

Dept. of Computer Science and Engineering 5


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

$ ls -l | wc –l Displays number of file in current directory

$ who | wc –l Displays number of currently logged in users

Creating a tee
tee is an external command that handles a character stream by duplicating its input. It saves one
copy in a file and writes the other to standard output. It is also a filter and hence can be placed
anywhere in a pipeline. Example:-

The following command sequence uses tee to display the output of who and saves this output in a
file as well.

$who | tee users.lst

Above command displays currently logged in users on standard output and writes a copy to
users.lst

Command substitution
The shell enables the connecting of two commands in yet another way. While a pipe enables a
command to obtain its standard input from the standard output of another command, the shell
enables one or more command arguments to be obtained from the standard output of another
command. This feature is called command substitution.

Example:

$echo Current date and time is `date`

Observe the use of backquotes around date in the above command. Here the output of the
command execution of date is taken as argument of echo. The shell executes the enclosed
command and replaces the enclosed command line with the output of the command. Similarly the
following command displays the total number of files in the working directory.

$echo “There are `ls | wc –l` files in the current directory”

Observe the use of double quotes around the argument of echo. If you use single quotes, the
backquote is not interpreted by the shell if enclosed in single quotes.

Dept. of Computer Science and Engineering 6


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

grep: Searching for a pattern

You often need to search a file for a pattern, either to see the lines containing ( or not containing)
it or to have it replaced with something else. This chapter discusses two important filters that are
specially suited for these tasks- grep and sed. This chapter also takes up one of the fascinating
features of UNIX – regular expressions (RE).

To discuss all the examples in this chapter we use following emp.lst as the reference file.

$ cat emp.lst

2233 | a. k. shukla | g. m. | sales | 12/12/52 | 6000

9876 | jai sharma | director | production | 12/03/50 | 7000

5678 | sumit chakrobarty | d. g. m. | marketing | 19/04/43 | 6000

2365 | barun sengupta | director | personnel |11/05/47 | 7800

5423 | n. k. gupta | chairman | admin | 30/08/56 | 5400

1006 | chanchal singhvi | director | sales | 03/09/38 | 6700

6213 | karuna ganguly | g. m. | accounts | 05/06/62 | 6300

1265 | s. n. dasgupta | manager | sales | 12/09/63 | 5600

4290 | jayant choudhury | executive | production | 07/09/50 | 6000

2476 | anil aggarwal | manager | sales | 01/05/59 | 5000

6521 | lalit chowdury | director | marketing | 26/09/45 | 8200

3212 | shyam saksena |d. g. m. | accounts | 12/12/55 | 6000

3564 | sudhir Agarwal | executive | personnel | 06/07/47 | 7500

2345 | j. b. saxena | g. m. | marketing | 12/03/45 | 8000

0110 | v. k. agrawal | g. m. | marketing | 31/12/40 | 9000

Dept. of Computer Science and Engineering 7


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

grep scans its input for a pattern displays lines containing the pattern, the line numbers or
filenames where the pattern occurs. The command uses the following syntax:

$grep options pattern filename(s)

grep searches for pattern in one or more filename(s), or the standard input if no filename is
specified.

The first argument (except the options) is the pattern and the remaining arguments are filenames.

Examples:

$ grep “sales” emp.lst

2233|a. k. shukla |g. m. |sales |12/12/52|6000

1006|chanchal singhvi |director |sales |03/09/38|6700

1265|s. n. dasgupta |manager |sales |12/09/63|5600

2476|anil aggarwal |manager |sales |01/05/59|5000

here, grep displays 4 lines containing pattern as “sales” from the file emp.lst.

$ grep president emp.lst #No quoting necessary here

$_ #No pattern (president) found

here, grep silently returns the prompt because no pattern as “president” found in file emp.lst.

$ grep “director” emp1.lst emp2.lst

emp1.lst: 9876|jai sharma |director |production |12/03/50|7000

emp1.lst: 2365|barun sengupta |director |personnel |11/05/47|7800

emp1.lst: 1006|chanchal singhvi |director |sales |03/09/38|6700

emp2.lst: 6521|lalit chowdury |director |marketing |26/09/45|8200

Here, first column shows file name.

when grep is used with multiple filenames, it displays the filenames along with the output.

Dept. of Computer Science and Engineering 8


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

grep options:
The below table shows all the options used by grep.

Option Significance
-i Ignores case for matching
-v Doesn't display lines matching expression
-n Displays line numbers along with lines
-c Displays count of number of occurrences
-l Displays list of filenames only
-e exp Matches multiple patterns
-f filename Takes patterns from file, one per line
-E Treats patterns as an ERE
-F Matches multiple fixed strings

Examples:

 Ignoring case (-i):

When you look for a name but are not sure of the case, use the -i (ignore) option.

$ grep -i 'agarwal' emp.lst

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

This locates the name Agarwal using the pattern agarwal.

 Deleting Lines (-v):

The -v option selects all the lines except those containing the pattern.

It can play an inverse role by selecting lines that does not containing the pattern.

$ grep -v 'director' empl.lst

2233|a. k. shukla |g. m. |sales |12/12/52|6000

5678|sumit chakrobarty |d. g. m. |marketing |19/04/43|6000

5423|n. k. gupta |chairman |admin |30/08/56|5400

Dept. of Computer Science and Engineering 9


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

6213|karuna ganguly |g. m. |accounts |05/06/62|6300

1265|s. n. dasgupta |manager |sales |12/09/63|5600

4290|jayant choudhury |executive |production |07/09/50|6000

2476|anil aggarwal |manager |sales |01/05/59|5000

3212|shyam saksena |d. g. m. |accounts |12/12/55|6000

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

2345|j. b. saxena |g. m. |marketing |12/03/45|8000

0110|v. k. agrawal |g. m. |marketing |31/12/40|9000

 Displaying Line Numbers (-n):

The -n(number) option displays the line numbers containing the pattern, along with the
lines.

$ grep -n 'marketing' emp.lst

3: 5678|sumit chakrobarty |d. g. m. |marketing |19/04/43|6000

11: 6521|lalit chowdury |director |marketing |26/09/45|8200

14: 2345|j. b. saxena |g. m. |marketing |12/03/45|8000

15: 0110|v. k. agrawal |g. m. |marketing |31/12/40|9000

here, first column displays the line number in emp.lst where pattern is found

 Counting lines containing Pattern (-c):

How many directors are there in the file emp.lst?

The -c(count) option counts the number of lines containing the pattern.

$ grep -c 'director' emp.lst

Dept. of Computer Science and Engineering 10


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

 Matching Multiple Patterns (-e):

With the -e option, you can match the three agarwals by using the grep like this:

$ grep -e “Agarwal” -e “aggarwal” -e “agrawal” emp.lst

2476|anil aggarwal |manager |sales |01/05/59|5000

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

0110|v. k. agrawal |g. m. |marketing |31/12/40|9000

 Taking patterns from a file (-f):

You can place all the patterns in a separate file, one pattern per line.

Grep uses -f option to take patterns from a file:

$ cat patterns.lst

director

manager

chairman

$ grep -f patterns.lst emp.lst

9876|jai sharma |director |production |12/03/50|7000

2365|barun sengupta |director |personnel |11/05/47|7800

5423|n. k. gupta |chairman |admin |30/08/56|5400

1006|chanchal singhvi |director |sales |03/09/38|6700

1265|s. n. dasgupta |manager |sales |12/09/63|5600

2476|anil aggarwal |manager |sales |01/05/59|5000

6521|lalit chowdury |director |marketing |26/09/45|8200

Dept. of Computer Science and Engineering 11


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

BASIC REGULAR EXPRESSION (BRE)


Like the shell's wild-cards which matches similar filenames with a single expression, grep uses an
expression of a different type to match a group of similar patterns. Unlike shell's wild-cards, grep
uses following set of meta-characters to design an expression that matches different patterns.

If an expression uses any of these meta-characters, it is termed as Regular Expression (RE).

The below table shows the BASIC REGULAR EXPRESSION(BRE) character set-

Symbols or Expression Matches


* Zero or more occurrences of the previous character
g* Nothing or g, gg, ggg, gggg, etc.
. A single character
.* Nothing or any number of characters
[pqr] A single character p, q or r
[c1-c2] A single character withing ASCII range shown by c1 and c2
[0-9] A digit between 0 and 9
[^pqr] A single character which is not a p, q or r
[^a-zA-Z] A non-alphabetic character
^pat Pattern pat at beginning of line
pat$ Pattern pat at end of line
^bash$ A bash as the only word in line
^$ Lines containing nothing

Examples:

 The character class


A RE lets you specify a group of characters enclosed within a pair of rectangular
brackets, [ ], in which case the match is performed for a single character in the group.

$ grep '[aA]g[ar][ar]wal' emp.lst

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

0110|v. k. agrawal |g. m. |marketing |31/12/40|9000

Dept. of Computer Science and Engineering 12


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

 The *
The * (asterisk) refers to the immediately preceding character.

Here, it indicates that the previous character can occur many times, or not at all.

$ grep '[aA]gg*[ar][ar]wal' emp.lst

2476|anil aggarwal |manager |sales |01/05/59|5000

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

0110|v. k. agrawal |g. m. |marketing |31/12/40|9000

 The Dot
A . matches a single character.

The pattern 2... matches a four-character patten beginning with a 2.

The pattern .* matches any number of characters, or none.

$ grep 'j.*saxena' emp.lst

2345|j. b. saxena |g. m. |marketing |12/03/45|8000

 Specifying pattern locations ( ^ and $ )


^ (carat) – For matching at the beginning of a line

$ (dollar) – For matching at the end of a line

$ grep '^2' emp.lst

2233|a. k. shukla |g. m. |sales |12/12/52|6000

2365|barun sengupta |director |personnel |11/05/47|7800

2476|anil aggarwal |manager |sales |01/05/59|5000

2345|j. b. saxena |g. m. |marketing |12/03/45|8000

Dept. of Computer Science and Engineering 13


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

$ grep '7...$' emp.lst

9876|jai sharma |director |production |12/03/50|7000

2365|barun sengupta |director |personnel |11/05/47|7800

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

 To display all lines that don't begins with a 2

$ grep '^[^2]' emp.lst

9876|jai sharma |director |production |12/03/50|7000

5678|sumit chakrobarty |d. g. m. |marketing |19/04/43|6000

5423|n. k. gupta |chairman |admin |30/08/56|5400

1006|chanchal singhvi |director |sales |03/09/38|6700

6213|karuna ganguly |g. m. |accounts |05/06/62|6300

1265|s. n. dasgupta |manager |sales |12/09/63|5600

4290|jayant choudhury |executive |production |07/09/50|6000

6521|lalit chowdury |director |marketing |26/09/45|8200

3212|shyam saksena |d. g. m. |accounts |12/12/55|6000

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

0110|v. k. agrawal |g. m. |marketing |31/12/40|9000

 To display only directories using ls -l and grep

$ ls -l | grep '^d'

drwxr-xr-x 2 chandrakant chandrakant 4096 Jan 16 12:18 Desktop

drwxr-xr-x 2 chandrakant chandrakant 4096 Jan 13 07:54 Documents

drwxr-xr-x 7 chandrakant chandrakant 4096 Jan 16 09:41 Downloads

Dept. of Computer Science and Engineering 14


Subject Subject Code Module 3
UNIX Shell Programming 15CS35 vi Editor and Shell

EXTENDED REGULAR EXPRESSION (ERE) AND


egrep
ERE make it possible to match dissimilar patterns with a single expression.

grep uses ERE characters with -E option.

egrep is another alternative to use all the ERE characters without -E option.

This ERE uses some additional characters set shown in below table-

Expression Significance
ch+ Matches one or more occurrences of character ch
ch? Matches zero or one occurrence of character ch
exp1 | exp2 Matches exp1 or exp2
GIF | JPEG Matches GIF or JPEG
(x1|x2)x3 Matches x1x3 or x2x3
(hard|soft)ware Matches hardware or software

Examples:

 The + and ?

+ - Matches one or more occurrences of the previous character

? - Matches zero or one occurrence of the previous character.

$ grep -E “[aA]gg?arwal” emp.lst

2476|anil aggarwal |manager |sales |01/05/59|5000

3564|sudhir Agarwal |executive |personnel |06/07/47|7500

 Matching Multiple Patterns( |, ( and ) )

$ grep -E 'sengupta|dasgupta' emp.lst

2365|barun sengupta |director |personnel |11/05/47|7800

1265|s. n. dasgupta |manager |sales |12/09/63|5600

$ grep -E '(sen|das)gupta' emp.lst

2365|barun sengupta |director |personnel |11/05/47|7800

1265|s. n. dasgupta |manager |sales |12/09/63|5600

Dept. of Computer Science and Engineering 15

You might also like