0% found this document useful (0 votes)
2 views

Lesson 07 - Editing Text Files and Search Patterns

This document provides an overview of using the vi/vim text editor in Linux, including how to create, edit, and navigate files. It covers essential commands for file operations, such as saving, quitting, copying, cutting, and searching for text. Additionally, it introduces the grep and egrep commands for pattern matching in files.

Uploaded by

Vrushank Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lesson 07 - Editing Text Files and Search Patterns

This document provides an overview of using the vi/vim text editor in Linux, including how to create, edit, and navigate files. It covers essential commands for file operations, such as saving, quitting, copying, cutting, and searching for text. Additionally, it introduces the grep and egrep commands for pattern matching in files.

Uploaded by

Vrushank Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Ubuntu Fundamentals

Editing Text Files and Search Patterns


Learning Objectives

By the end of this lesson, you will be able to:

Create files using the vi editor

Copy or cut and paste data in the vi editor

Apply different file operations using the vi editor

Use grep or egrep for pattern matching


Introduction to vi/vim Editor

• Vi editor is the most used text editor in Linux.


• It has been modified by the open-source
community and the new version is known as vim.
• Vi editor is also the default editor in Linux
distributions.
Different Modes of vi Editor

Vi editor works in three different modes:


• Command mode is used for navigation.
• Input mode is used to enter and replace text.
• Insert mode is used for pattern matching,
saving, and quitting.
Create and Assign File Name

• Use the vim command on terminal to open the vi editor.


• The default mode is command mode.
Switch from Command Mode to Insert Mode

The different keyboard characters used to switch from command mode to insert mode are given
below:

Option Description
i Inserts data before the current character
I Inserts data from the start of the current line
a Inserts data after current character
A Inserts data at the end of the line
o Adds data one line below the cursor
O Adds data one line above the cursor
Save and Quit File

• Shift colon key is used to enter into the command


prompt.
• wq command is used to save and quit the file.
Copy and Paste Data

• The yy command is used at the beginning of


the data or line to copy.
• The numerical value before the command yy is
used to indicate the number of lines of data to
be copied.
• The p command is used to paste the copied
data in the required destination.
Cut and Paste Data

• The dd command is used to cut the data


or line.
Delete Character, Word, and Line

• The x key is used to delete the selected


character.

• The dw command is used to delete the


selected word.
• The dd command is used to delete a line of
data.
Search Word and Character

The ? or / key is used to search for any specific string in the vi editor.
Jump a Line

• The G command is used to jump to the


last line.
• The gg command is used to move to the
first line.
Join a Line

The J command is used in the current line of data to join it with the next line of
data.
Navigate Through Files

The following keyboard characters are used to move around


the file:
• h is used to move left
• j is used to move down
• k is used to move up
• l is used to move right
grep and egrep Command

• grep stands for global regular expression print and


is used to find the pattern in a given file.
• egrep stands for extended global regular
expressions print.
• egrep is faster than grep.
grep and egrep Command

The following options can be used along with grep command to perform the mentioned tasks:

Option Description
-o Output the matched parts of a matching line
-v Invert the match
-c Display the count of matching lines
-l Display the list of filenames
-n Display the matched lines and their line numbers
Key Takeaways

Vi editor is also the default editor in Linux distributions.

The number at the beginning of the yy command describes


the number of lines that need to be copied.

The dw command is used to delete a word.

grep stands for global regular expression print.


Knowledge Check
Knowledge
Check
Which command is used to save and quit the file in vi editor?
1

A. q

B. wq

C. mv

D. None of them
Knowledge
Check
Which command is used to save and quit the file in vi editor?
1

A. q

B. wq

C. mv

D. None of them

The correct answer is B

w is used to save a file, and q is used to quit. Therefore, wq is used to save and quit a file.
Knowledge
Check
Which command is used to copy the current line of the file?
2

A. yy

B. yw

C. yc

D. yz
Knowledge
Check
Which command is used to copy the current line of the file?
2

A. yy

B. yw

C. yc

D. yz

The correct answer is A

The yy command is used to copy current line of the file.


Knowledge
Check
Which command is used to search for a string in the file using vi editor?
3

A. / or ?

B. s

C. F

D. t
Knowledge
Check
Which command is used to search for a string in the file using vi editor?
3

A. / or ?

B. s

C. F

D. t

The correct answer is A

/ or ? operator is used to search for a string inside the file.


Knowledge
Check
Which command is used to jump to the first line?
4

A. J

B. F

C. GG

D. gg
Knowledge
Check
Which command is used to jump to the first line?
4

A. J

B. F

C. GG

D. gg

The correct answer is D

The gg command is used to jump to the first line of the data.

You might also like