Nothing Special   »   [go: up one dir, main page]

Ulp 3 and 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

2321020

Practical No.3
Aim: To familiarize with Linux/Unix grep filter commands.

1. The Asterisk character (*): It is used to match multiple characters. It


stands for zero or more occurrences of preceding characters. For example, the expression
“a*” matches ^(null), a, aa, aaa... and so on. Here null stands for zero occurrences of
character “a”.

2. Dot Character(.): The (.) Dot is used to match a single character except a new
line character. (.) Dot is used in regular expression whereas in case of shell operator “?” is
used to match a single character.
2321020

3. Plus character (+): The + character is similar to (*) except that (+) character
matches one or more occurrences of preceding character.

4. Caret character (^): This quantifier is used to search those words that
start with a specified pattern. And it is used for negate the symbol.

Exp1: “a” matches “a” at the start of the line


Exp2: “[^0-9]” matches any non digit.

5. Dollar character ($): It matches or search those words that ends with
specified pattern.

6. Braces { }: curly bracket are used as range.


2321020

7. $ls -l | grep “^d”: This command displays the all directories under the current
working directory.

8. $ ls -l | grep “^-rwx”: This command display all the files which have rwx
permission for the user.

9. grep pattern filename : This command is used for searching the single
pattern into the file.

10.Searching Multiword String: It is used for searching multiword string


or multiword string pattern into the file.

11. Inverse Option (-v): It will print those lines that do not contains specified
patterns.
2321020

12. Ignore option (-i): Grep distinguish between uppercase and lowercase with -I
option it will prints all patterns without considering the case.

13.Filename option (-l): It print the filename in which specified pattern is


present.

14. Count occurrences (-c): The -c option with grep command will count the
occurrences of records.

15.Line number option (-n): This option will print out the line number with
the record that contain the specified pattern.
2321020
16.Multiple pattern search option (-e): This pattern option allows us
to search multiple patterns within a single command.

17. $ egrep “(raj|avni) greptest2”: This command is used to search a


pattern containing either “raj” or “avni” into the file.

18. $ egrep “raj(Kishor|komal)”: This command is used to search a pattern


starts with pattern “raj” and reminaing part is either “kishor” or ”komal”.

19. Egrep “e{2}*”: This command is used to search a pattern contains consecutive
“ee” characters.
2321020

20. fgrep ‘^’ greptest3: This command is used to print the result which start
with the ‘^’.

21. fgrep -F ‘^’ greptest: This command is used to print the result which start
with the ‘^’.

22. fgrep ‘$’ greptest: This command is used to print the result which ends with
the ‘$’.
2321020
23. fgrep -F ‘$’ greptest3: This command is used to print the result which ends
with the ‘$’.

24. fgrep ‘$$’ greptest3: This command is used to print the result which ends
with the ‘$$’.
2321020
PRACTICAL-04
AIM: To familiarize with the VI editor.
Theory: The default editor that comes with the UNIX operating system is called vi (visual
editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also
use this editor to just read a text file.

Syntax: vi filename

Modes of Operation in vi editor: There are three modes of operation in vi:

 Command Mode: When vi starts up, it is in Command Mode. This mode is


where vi interprets any characters we type as commands and thus does not display
them in the window. This mode allows us to move through a file, and to delete,
copy, or paste a piece of text. To enter into Command Mode from any other mode,
it requires pressing the [Esc] key. If we press [Esc] when we are already in
Command Mode, then vi will beep or flash the screen.

 Insert mode: This mode enables you to insert text into the file. Everything
that’s typed in this mode is interpreted as input and finally, it is put in the file. The
vi always starts in command mode. To enter text, you must be in insert mode. To
come in insert mode, you simply type i. To get out of insert mode, press the Esc
key, which will put you back into command mode.

 Last Line Mode (Escape Mode): Line Mode is invoked by typing


a colon [:], while vi is in Command Mode. The cursor will jump to the last line of
the screen and vi will wait for a command. This mode enables you to perform tasks
such as saving files, executing commands.
2321020
Starting the vi Editor: There are following way you can start using vi editor:

Commands and their Description


 vi filename: Creates a new file if it already not exists, otherwise opens
existing file.
 vi -R filename: Opens an existing file in read only mode.
 view filename: Opens an existing file in read only mode.
Moving within a File (Navigation): To move around within a file without
affecting text must be in command mode (press Esc twice). Here are some of the commands
can be used to move around one character at a time.

Commands and their Description

 k: Moves the cursor up one line.

 j: Moves the cursor down one line.


2321020
 l: Moves the cursor to the right one character position.

 h: Moves the cursor to the left one-character position.

 0 or | : Positions cursor at beginning of line.

 $: Positions cursor at end of line.

 W: Place cursor to the next word.


 B: Place cursor to the previous word.
 (: Positions cursor to beginning of current sentence.
 ): Positions cursor to beginning of next sentence.
 H: Move to top of screen.
 nH: Moves to nth line from the top of the screen.
 M: Move to middle of screen.
 L: Move to bottom of screen.
 nL: Moves to nth line from the bottom of the screen.
2321020
 :x: Colon followed by a number would position the cursor on line number represented
by x.
 ~: Case will be changed.

Control Commands (Scrolling):


There are following useful commands which can used along with Control Key:

Commands and their Description:


 CTRL+d: Move forward 1/2 screen.
 CTRL+f: Move forward one full screen.
 CTRL+u: Move backward 1/2 screen.
 CTRL+b: Move backward one full screen.
 CTRL+e: Moves screen up one line.
 CTRL+y: Moves screen down one line.
 CTRL+u: Moves screen up 1/2 page.
 CTRL+d: Moves screen down 1/2 page.
 CTRL+b: Moves screen up one page.
 CTRL+f: Moves screen down one page.
 CTRL+I: Redraws screen.
Editing and inserting in Files (Entering and Replacing Text): To edit the file, we need
to be in the insert mode. There are many ways to enter insert mode from the command
mode.

 i: Inserts text before current cursor location.


2321020

 I: Inserts text at beginning of current line.


 a: Inserts text after current cursor location.
 A: Inserts text at end of current line.
 o: Creates a new line for text entry below cursor location.
 O: Creates a new line for text entry above cursor location.
 r: Replace single character under the cursor with the next character typed.
 R: Replaces text from the cursor to right.
 s: Replaces single character under the cursor with any number of characters.
 S: Replaces entire line.
Deleting Characters: Here is the list of important commands which can be used to
delete
characters and lines in an opened file.

 X Uppercase: Deletes the character before the cursor location.


 x Lowercase: Deletes the character at the cursor location.
 dw: Deletes from the current cursor location to the next word.
 d^: Deletes from current cursor position to the beginning of the line.
 d$: Deletes from current cursor position to the end of the line.
 Dd: Deletes the line the cursor is on.
Copy and Paste Commands: Copy lines or words from one place and paste them on
another place by using the following commands.

 yy: Copies the current line.


 9yy: Yank current line and 9 lines below.
 p: Puts the copied text after the cursor.
 P: Puts the yanked text before the cursor.
Save and Exit Commands of the ex-Mode: Need to press [Esc] key followed by the
colon (:) before typing the following commands:

 q: Quit
 q!: Quit without saving changes i.e. discard changes.
 r fileName: Read data from file called fileName.
 wq: Write and quit (save and exit).
 w fileName: Write to file called fileName (save as).
 w! fileName: Overwrite to file called fileName (save as forcefully).
2321020
 !cmd: Runs shell commands and returns to Command mode.
 Save and quit the file:

 Return to the terminal:


2321020

You might also like