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

Vi Editor

This document provides information about common Linux commands like head, tail, sort, grep, ps and vi. It explains how to use these commands, their basic syntax and examples of common uses.
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 views

Vi Editor

This document provides information about common Linux commands like head, tail, sort, grep, ps and vi. It explains how to use these commands, their basic syntax and examples of common uses.
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/ 61

Filters

Using head to look at the top of a file

$ head [-number][file name]

Line number

Without a line number, head will display


the first ten lines of a file. If the line
number exceeds the total number of
lines actually present in the file, head
will display the entire file.
• Example:

$ cat record
aaaa
bbbb
cccc
dddd
$ head –3 record
eeee aaaa
bbbb
cccc
Using tail to look at the bottom of a
file

$ tail [ + line number][file]

Note:

When using the (-) line count option, tail cannot display
segments consisting of more than 4096 characters. If you
specify more than 4096 characters, only the last 4096 bytes
will be displayed.
Example:

$ cat report
1.aaa
2.bbb
3.ccc
+
4.ddd
5.eee

$ tail +2 report $ tail –2 report


bbb ddd
ccc eee
ddd
eee
Using sort to display a file in order

$ sort[options][field-specifier
list][file name]
Option Description

-k To sort on a certain column.

-f Fold lower case into upper case.


(treats uppercase letters as if they
were lowercase).
-r Reverse the sense of the sort ( z
precedes a )
• Example:

$ cat list $ sort list


English
1.student
Science
2.student
maths
history
$ sort –r list

$ sort –f list
$ cat list
Nimal Perera 2000
Ruwani Silva 1000
Sunil Yapa 3400
Amali Silva 1500
Field no.
$ sort -k2 list
Nimal Perera 2000
Ruwani Silva 1000
Amali Silva 1500
Sunil Yapa 3400
$ sort –k3 list
Ruwani Silva 1000
Amali Silva 1500
Nimal Perera 2000
Sunil Yapa 3400
Using grep to find a string

$ grep [options] [pattern] [file


- list]

The grep (global regular expression print) utility


searches through a file to see if it contains the
specified string of characters and writes these
lines to the standard O/P.
Option Description

-c Displays the count of the number of


occurrences
-l Displays the list of file names only

-n Displays line numbers along with the lines

-v Reverse sense of the test, displays lines


not containing a match
• Example I:

$ cat memo
Nimal,
In our meeting on June 6th ,
we discussed the issue of credit.
Have you had any further
thought about it?
Pattern to be searched
File name
$ grep credit memo
we discussed the issue of credit.

Output

$ grep it memo -> what will be


the output?
I.grep -n accountant emp1
3: emp1: …accountant…
10: emp1: ..accountant …

Line number

I.
grep -l 'accountant' *
emp1
… File list
emp4
$ grep accountant emp1 emp2
emp1: …… accountant ….
emp1: …… accountant ….
emp2: ….accountant …..

To search for the string DCSD Course in the


file NIBM:
$ grep DCSD Course NIBM
-> what will be the output?
$ grep “DCSD Course” NIBM
Using –v Option

$ grep –v it memo
In our meeting on June 6th ,
Have you had any further
Standard Input and Standard
Output
• UNIX assumes the input derived from what
you type on the keyboard as the standard
input.
• The result of a command, which is sent by
default to a user's terminal is called the
standard output.
• Therefore, when you login, the shell
automatically sets the standard input (file) to
be your keyboard and the standard output
(file) to be your screen.
Redirection
There are different ways in which the input or
the output of a command can be redirected .
ie. Taken from or sent to other files besides
the standard input/output files.

One way to redirect the input or the output to


or from a file is to use the Redirection
symbols. Redirection symbols are most often
used to separate the output destination.
Another way to redirect I/O is to pipe the
output of one command to another. Ie O/P of
one command becomes the I/P of another.
Redirection Symbols

• Input redirection:
• The symbol < is used to read a
command's I/P from a file instead of the
standard I/P.
Command < filename

Example:
$ mail john < report
• Output redirection:
• The symbol > and >> redirects the O/P
of a command into a file.
• > - Writes the O/P of a command to the
named file overwriting any existing data.
If the file does not exists, UNIX will first
create it.
• >> - Appends the O/P of a command to
the named file if it already exists.
• Example:
$cat > filename
$cat chapter1 chapter2> newchapt

Note:
Only the lines will be copied and I/P files
will all exist as separate files.
Pipes
• The pipe key is used to send the output of
one command to the input of another. You
can use several pipes in a single command
line, so you can combine as many commands
as you want.

• Example:
To count the number of files in the current
directory:
$ ls *.c | wc -l
15
• How would you write the command
who | users.log so that it
executes properly?
Splitting a pipeline with tees: The tee
utility

• You can use the tee utility in a pipe to


send the output of a command to a file
while also sending it to its standard
output or to another device.

tee [ -a ][ file name(s)]


• Example:

$ cat newnames oldnames | grep NIBM |


tee record | lpr

Splits the pipe


File-> record

Output
Printer
• What is the effect of the following
command sequences?
• who | sort | lpr
• who | tee users.log |grep mala
• sort –f -k2 tplist
• cat Contacts > book.2000
Running a Program in the
background

As UNIX is a multi tasking OS, it lets a user to


do more than one job at a time. since there
can only be one job in the foreground, the
rest of the jobs have to be run in the
background.

To run a command in the background, type


an ampersand (&) just before the RETURN
that ends the command line.
Example:

$ ls -l | lpr &
[1] 71
PID

On invoking a command with the &, the shell


returns a number called the PID (process
identifier) number, in this case 71.
The process status- ps command

• The ps command is used to display the


characteristics of a process. The
command invoked without options will
list out the processes associated with a
user at a particular terminal.
Process time that has been
Example I: consumed since the process
started
$ ps
PID TTY TIME COMMAND
30 01 12.04 ksh
56 01 12.05 ps
• -u option:
• Lets you know the activities of any user at
any time.
Process time that has been
Example I: consumed since the process
$ ps started

PID TTY TIME COMMAND


30 01 12.04 sh
56 01 12.05 ps

Example II:
$ ps –u nimal User name

PID TTY TIME COMMAND


31 02 0:02 sh
59 02 0:03 grep
Premature Termination of a process

kill - command aborts a process you


are running in the background.

$ kill 0 - Kill all processes running in


the background

$ kill 7725 - Kill the process 7725

PID
• Note:
If you forget the PID number, you can
use the ps utility to display it.
vi Editor
• vi is a full screen editor, available with
all UNIX systems and is an essential
tool. It is one of the most powerful
editors available in any environment.

• The editor can be used to create and


edit files.
The three modes in vi
:
Command Last line
mode mode

RETURN

ESCAPE a, A, I, I
o,O

Input
mode
• Input Mode: Where any key depressed
is entered as text.

• Command Mode: Where keys are used


as commands to act on text

• Last line Mode: Where the commands


can be entered in the last line to act on
text
The Buffer

While you edit a file with vi, you do not


actually change the file itself. The
changes you make are written to the
buffer, which is a copy of the file held in
the system memory rather than in the
disk storage system.
Starting vi

$ vi [file name]

Example: $ vi practice

Note:
When you open a file you are said to be
in the command mode.
Searching for a String
1. /and
Finds the next occurrence of the string ‘and’.

Finds occurrences such as:


sand, and , standard, stand

2. /\<and\>
3. ?and
Find the previous occurrence of the string
'and'
4. /^ the
Find the next line that starts with ‘the'
Substituting one string for another

Syntax:

: [address] s/search string/ replace string /[g]


Address Portion of work buffer addressed
5 Line number 5
77,100 Line 77 through 100 inclusive
1,$ The entire work buffer
Replace string
Examples:

1. :s/bigger/biggest

Search string

Replaces the string 'bigger' on the current line


with 'biggest'
Global flag

2.:1,$s/ten/10/g

Replaces every occurrence of the string ten by


10 in the entire work buffer.

Global flag - replaces all occurrences

Replaces occurrences such as:

ten -> 10 often ->of10 tenant ->10ant


3. :1,$s/\<ten\>/10/g

Replaces every word occurrence of


'ten’ by the string 10.

Example: ten ->10


Copying and pasting

• Commands used:
• yy – copy (yank)
• p - paste

Bring the cursor to the line of text you want


to copy and type 'yy'. (make sure you are in
the command mode). Then move the cursor
to the place where you want the text placed
and type 'p'.
• Example:

7yy- copies seven lines of text


beginning with the cursor line.

7 yy

No. of lines to be copied


Cutting and pasting

• Commands used:
• dd – cut
• p – paste

Example:
10dd – cut 10 lines starting from the
current line.
Using lettered buffers

The vi editor maintains a set of lettered


(a-z) buffers where you can store
yanked or deleted blocks of text.
Examples:

• To copy a line of text to the buffer ‘b’


"byy

Buffer name

• To copy 5 lines of text to the buffer ‘q’


"q5yy
Accessing a lettered buffer
Example:

• "ap - copy the text stored in buffer 'a'


Saving deleted text in a lettered
buffer

Example:

• "bdd - delete the current line of text and


place it in the buffer 'b‘.

• “g5dd - delete five lines starting from


the current line and place it in buffer 'g'.

You might also like