Experiment 3

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

EXP:3

Demonstrate the usage of pipes and filters using Linux commands

Aim/Objective:
Demonstrate the usage of pipes and filters using Linux commands - wc, cut,
paste,comm, uniq, more, less, cmp, diff, sort, files, uniq, grep

Apparatus Required:
Hardware Requirements – Intel core
II CPU Software Requirements –
Red-hat Linux
Theory
Pipes
Pipes is used to give the output of one command as input to another
command. Command is piped together using vertical bar ‘|’. When a pipe is
established between two commands the output of first command is redirected to
standard input of second command.
Syntax
Command 1|command 2
Filters
Any program that can take input from standard input and write result to
standard output is called filter. Linux has a number of filters

Procedure

Command : sort (ordering a file)


Usage : It is used to sort re-orders lines in ASCII collating sequences,
white spaces, then numerals, uppercase and finally lowercase.
Syntax : sort [-options] filename
Options: -r display input taken from the reverse alphabetical order
-f output arranged in alphabetical order
-n arrange the input according to numerical
values Example: $ sort –n sample.txt

Command: grep
Usage : This command is used to search files for lines that match a
given pattern. Syntax : grep file pattern
Example : grep sample.txt

Command : cut
Usage : (cutting columns)
This command can be used to cut the columns from a file with
-c option. Syntax :$ cut -c [numbers delimited by comma or
range] <file name> Option : -f option you can cut the fields
delimited by some character.
-d option is used to specify the delimiter and -f option used
to specify the field number.
Example : $ cut -c 1, 2, 3-5
students.txt
Command : paste
Usage : The paste command will paste the contents of the file
side by side Syntax : $ paste <file1 name> <file2 name> <filen
name>
Example: $ paste cutlist1.txt
cutlist2.txt
Command: wc (statistic of file)
Usage : This command is used to count lines, words and characters,
Depending on the option used.
Syntax : wc [options] [file
name] Options : -l: Number
of lines
-w: Number of words
-c: Number of
characters Example : $
wc sample1.txt
Output : 65 2776 17333 sample1.txt
Which means sample1.txt file has 65 lines, 2776 words, and 17333 Characters.

Command: cmp
Usage : This command is used to compare two files whether they are
identical or not.
• The two files are compared byte by byte and the location
of the first mismatch is printed on the screen.
• If two files are identical, then it does not print anything on
the screen Syntax : cmp <file1> <file2>
Example: $ cmp sample1.txt sample2.txt

Command : less

Usage: Less command is a Linux utility that can be used to read the contents of a
text file one page (one screen) at a time.
Syntax:less [options] filename
Example:less welcome.txt

Command :more

Usage: more command is used to view the text files in the command prompt,
displaying one s creen at a time in case the file is large (For example log
files).
Syntax:more [-options] [file_name]
[-options]: any option that you want to use in order to change the
way the file is displayed. Choose any one from the followings: (-d, -l, -f, -
p, -c, -s, -u)
-d : Use this command in order to help the user to navigate. It displays
“[Press space to continue, ‘q’ to quit.]” and displays “[Press ‘h’
for instructions.]” when wrong key is pressed.
-f : This option does not wrap the long lines and displays them as such.
-p : This option clears the screen and then displays the text.
-c : This command is used to display the pages on the same area by
overlapping the previously displayed text.
-s : This option squeezes multiple blank lines into one single blank line.
-u : This option omits the underlines.

Example:more -d

sample.txt Command: uniq

Usage:The uniq command in Linux is a command-line utility that reports or


filters out the repeated lines in a file.
Syntax: uniq [file_name]

Example: $uniq kt.txt

Command: file

Usage:file command is used to determine the type


of a file. Syntax:file <filename>

Example: file sec.py

output : sec.py: Python script, ASCII text executable

You might also like