You are on page 1of 24

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : A V N L Sarojini
Designation : Lecturer
Branch : Computer Engineering
Institute : A.A.N.M. & V.V.R.S.R. Poly.,
Gudlavalleru.
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM-302
Topic : Shell Programming &
Filtering Techniques
Duration : 50 Min
Sub Topic : Filtering Techniques
Teaching Aids :PPT

CM304.19 1
Objective

On completion of this period, you would be able to


know
 Introduction to Filtering Techniques
 head command
 tail command
 nl command

CM304.19 2
Filtering Techniques

 The filter command namely head, tail, cut, paste


etc., accepts some data as input performs
manipulation on it and produces some data as
output, since they perform some filtering action on
data they are called filters.

CM304.19 3
head command

 The head command displays the top of the file. The


simplest use of the command involves specification
of the filename, without any arguments.

Syntax:
$head [no.of lines] [file name]

CM304.19 4
head command

 When the head command is used, without specifying


the no. of lines. It displays the first ten lines of the
file.

Example : $head temp.lst

Displays the top ten lines of the file temp.1st

CM304.19 5
head command

 You can specify a line count, say the first three lines
of the file. Use the –symbol followed by a numeric
argument.

Example: $head -3 emp.1st

CM304.19 6
head command

 head command also has the capability to work with


multiple files.

Ex: $head -3 temp.1st student.1st

CM304.19 7
tail command

 The tail command displays the end of the file it is an


additional method for addressing lines and can also
extract information in units of block and characters.

 The tail command displays last 10 lines of the file by


default.

Example: $tail temp.1st

CM304.19 8
tail command

 We can specify a count to extract the required number


of lines with tail command.

Example: $tail -2 temp.1st

CM304.19 9
tail command

 You can address lines from the beginning of the


file, instead of the end. The +line count option
allows you to do that, where ‘line count’
represents the line number from where the
selection should begin.

Example: $tail +11 emp.1st

CM304.19 10
tail command

 To extract last 15 characters of the file use -c option


with the tail command.

Example: $tail -15c temp.lst

CM304.19 11
tail command

 To extract block of data use -b option with the tail


command.

Example: $tail -1b temp.lst

CM304.19 12
n1 command

 The ‘nl’ command enables for numbering lines. By


default ‘nl’ simply add line numbers to its output and
print them in a space of 6 character wide.

Example: $nl temp

CM304.19 13
nl command

 -w : we can use -w(width) option to specify the width


of the number format.

Example: $nl -w2 temp

CM304.19 14
nl command

-s (separator): used to specify the separator.

$n1 -s”|” temp

-v: -v followed by a number determines the initial value


that is to be used to number the lines.

$n1 -v200 xyz

CM304.19 15
nl command

-i (increment) : you can set the increment with the ‘-i’


option.

Example: $n1 -v200 -t5 xyz

CM304.19 16
Summary

 The head command displays the top of the file.

 The tail command displays the end of the file also


discusses additional method for addressing lines and
can also extract information in units of block and
characters.
 The nl command enables for numbering lines.

CM304.19 17
Quiz

1.Without specifying the number of lines, the head


command, by default displays top_______ lines.

(a) 20
(b) 11
(c) 10
(d) 15

CM304.19 18
Quiz

1.Without specifying the number of lines, the head


command, by default displays top_______ lines.

(a) 20
(b) 11
(c) 10
(d) 15

CM304.19 19
Quiz

2.Without specifying the number of lines, the tail


command, by default will displays _______ lines.

(a) 20
(b) 11
(c) 10
(d) 15

CM304.19 20
Quiz

2.Without specifying the number of lines, the tail


command, by default will displays _______ lines.

(a) 20
(b) 11
(c) 10
(d) 15

CM304.19 21
True/False

1.tail +11 emp.lst is a valid tail command [TRUE/FALSE]

CM304.19 22
True/False

1.tail +11 emp.lst is a valid tail command [TRUE/FALSE]

CM304.19 23
Frequently Asked Questions

1. Differentiate between head and tail commands with


suitable examples.
2. Explain briefly about ‘nl’ command and its various
options.

CM304.19 24

You might also like