Open In App

Pipes and Filters in Linux/Unix

Last Updated : 01 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Input: ls|more
  • Output: more command takes input from ls command and appends it to the standard output. It displays as many files that fit on the screen and highlighted more at the bottom of the screen. To see the next screen hit enter or space bar to move one line at a time or one screen at a time respectively.

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'.

grep-command-linux 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

sort-command-linux 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:

  • To display next line, press the enter key
  • To bring up next screen, press spacebar
  • To move to the next file, press n
  • To quit, press q.

Syntax:

$more[options] filename

Example:

cat fruits.txt | more

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads