CM304

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

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-304
Topic : Shell Programming &
Filtering Techniques
Duration : 50 Min
Sub Topic : Filtering Techniques
Teaching Aids : PPT

CM304.21 1
Objective

On completion of this period you would be able


to know

 sort command
 unique command
 tee command

CM304.21 2
Recap

 You can slice a file vertically with the cut command.

 The information extracted with the cut command can


be pasted back with the paste command. It resembles
the cat command but cat pastes more than one file
horizontally, paste does it vertically.

 pg command allows display of files one display page


at a time on the screen.

CM304.21 3
sort command

 UNIX sort command is different from other sort


commands in the sense that lines need not have a fixed
length for it to work successfully.

 When sort command is invoked without options it sorts


the entire line. Sorting starts with the first character of
line and proceeds to the next character only when the
characters in the two lines are identical.

CM304.21 4
sort command

 The sequence followed by the sort command is first it


checks for wide space, numericals, uppercase letters
and finally lowercase letters.

CM304.21 5
sort command options

Options Description
-t (char) Uses delimiter character to identify fields
-o
Places output in file filename
filename
-m file Merges sorted files in file.
+k Starts sorting after skipping the kth field.

CM304.21 6
sort command options

Options Description
-k Starts sorting after the kth field.
-r Reverses sort order.
-n Numeric order.

CM304.21 7
sort command

 Sort also works on fields, the default field separator is


the space character. The -t option followed by the
delimiter overwrites the default delimiter.

Example: $sort -t”|” +2 student

The argument +2 indicates that sorting should start


after skipping the first two fields.

CM304.21 8
sort command

 Sort command is one of the few UNIX filters which


also accepts the output filename as an argument. This
is done with the ‘-o’ option.

Example: $sort -o +2 student abc

CM304.21 9
sort command
 When sort acts on numbers it sorts the numbers by
ASCII collating sequence. This can be over ridden
by the ‘-n’ option.
$cat > temp $sort temp $sort -n temp
1 1 1
4 10 4
24 24 8
8 4 10
10 8 24
CM304.21 10
sort command

Duplicate lines are removed with the -u option.

Example: $sort -u filename

CM304.21 11
unique command
UNIX offers a special tool to handle duplicate
records using uniq command.
Example: cat > temp $sort -o temp temp|uniq
1 swaroop
1 Swaroop
2 anand
3 sateesh 2 anand
5 venu 3 sateesh
2 sai ram
5 venu
3 padmaja

CM304.21 12
unique command

UNIX simply fetches one copy of the redundant


record and write them on to standard output, unique
requires a sorted file as a input.

CM304.21 13
unique command

-u option:

The -u option selects only non repeated values.

Example: $unique -u temp


1 Swaroop
5 Aenu

CM304.21 14
unique command

-c option: It displays the frequency of occurrence of


all lines along with the line.

Example: $unique -c temp

CM304.21 15
tee command

UNIX provides a feature by which you can save the


standard output in a file, as well as display it on the
terminal (or pipe into an another command) this is
made possible by the ‘tee’ command.

‘tee’ command doesn’t perform any filtering action


on its input, it gives out exactly what it takes.

CM304.21 16
tee command

Example: Write a command to save who command


output into a file usr.1st and display it on the screen.

$who | tee usr.1st


$who |tee usr.1st | wc -l
$who | tee usr.1st | tee count
$who | tee /dev/try | wc -l

CM304.21 17
Summary

 UNIX provides sort command to sort the contents of a


file.
 UNIX offers a special tool to handle duplicate records
using unique command.
 UNIX provides a feature by which you can save the
standard output in a file, as well as display it on the
terminal (or pipe into a another command) this is made
possible by the tee command.

CM304.21 18
Quiz

1.The sequence followed by the sort command is first it


checks for numerical, white space, uppercase letters
and finally lowercase letters.

[TRUE / FALSE]

CM304.21 19
Quiz

1.The sequence followed by the sort command is first it


checks for numerical, white space, uppercase letters
and finally lowercase letters.

[TRUE / FALSE]

CM304.21 20
Quiz

2.Unique command requires a sorted file as input.

[TRUE/FALSE]

CM304.21 21
Quiz

2.Unique command requires a sorted file as input.

[TRUE/FALSE]

CM304.21 22
Quiz

3.Tee command doesn’t perform any filtering action on


its input, it gives out exactly what it takes

[TRUE/FALSE]

CM304.21 23
Quiz

3.Tee command doesn’t perform any filtering action on


its input, it gives out exactly what it takes

[TRUE/FALSE]

CM304.21 24
Frequently Asked Questions

1. Explain briefly about ‘sort’ command and its various


options.
2. Explain briefly about ‘unique’ command and its
various options.
3. Explain briefly about ‘tee’ command and its various
options.

CM304.21 25

You might also like