Open In App

Pipes and Filters in Linux/Unix

Pipes in UNIX

The novel idea of Pipes was introduced by M.D Mcllroy in June 1972– version 2, 10 UNIX installations. Piping is used to give the output of one command (written on LHS) as input to another command (written on RHS). Commands are piped together using vertical bar “ | ” symbol. Syntax:

command 1|command 2

Example:

Filters in UNIX

In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin, perform some operations and write output to standard output stream i.e. stdout. The stdin and stdout can be managed as per preferences using redirection and pipes. Common filter commands are: grep, more, sort



1. grep Command:It is a pattern or expression matching command. It searches for a pattern or regular expression that matches in files or directories and then prints found matches. 

Syntax:



$grep[options] "pattern to be matched" filename 

Example:

Input : $grep 'hello' ist_file.txt
Output : searches hello in the ist_file.txt and outputs/returns the lines containing 'hello'.

The Options in grep command are: Grep command can also be used with meta-characters

Example:

Input : $grep 'hello' *
Output : it searches for hello in all the files and directories.

* is a meta-character and returns matching 0 or more preceding characters 

2. sort Command: It is a data manipulation command that sorts or merges lines in a file by specified fields. In other words it sorts lines of text alphabetically or numerically, default sorting is alphabetical

Syntax:

$sort[options] filename

The options include: Example:

$sort fruits.txt
$sort -n grades.txt

3. more Command: It is used to customize the displaying contents of file. It displays the text file contents on the terminal with paging controls. Following key controls are used:

Syntax:

$more[options] filename

Example:

cat fruits.txt | more

While using more command, the bottom of the screen contains more prompt where commands are entered to move through the text.

Article Tags :