Vi Editor
Vi Editor
Line number
$ cat record
aaaa
bbbb
cccc
dddd
$ head –3 record
eeee aaaa
bbbb
cccc
Using tail to look at the bottom of a
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
$ sort[options][field-specifier
list][file name]
Option Description
$ 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
$ 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
Line number
I.
grep -l 'accountant' *
emp1
… File list
emp4
$ grep accountant emp1 emp2
emp1: …… accountant ….
emp1: …… accountant ….
emp2: ….accountant …..
$ 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.
• 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
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
$ ls -l | lpr &
[1] 71
PID
Example II:
$ ps –u nimal User name
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.
RETURN
ESCAPE a, A, I, I
o,O
Input
mode
• Input Mode: Where any key depressed
is entered as text.
$ 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’.
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:
1. :s/bigger/biggest
Search string
2.:1,$s/ten/10/g
• Commands used:
• yy – copy (yank)
• p - paste
7 yy
• Commands used:
• dd – cut
• p – paste
Example:
10dd – cut 10 lines starting from the
current line.
Using lettered buffers
Buffer name
Example: