Pipes and Filters in Linux/Unix
Last Updated :
01 Nov, 2022
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.
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:
- 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
While using more command, the bottom of the screen contains more prompt where commands are entered to move through the text.
Similar Reads
Filters in Linux Filters are programs that take plain text(either stored in a file or produced by another program) as standard input, transforms it into a meaningful format, and then returns it as standard output. Linux has a number of filters. Some of the most commonly used filters are explained below: 1. cat : Dis
3 min read
grep command in Unix/Linux The grep command is one of the most useful tools in Linux and Unix systems. It is used to search for specific words, phrases, or patterns inside text files, and shows the matching lines on your screen. Syntax of grep Command in Unix/LinuxThe basic syntax of the `grep` command is as follows:grep [opt
6 min read
Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak
8 min read
Piping in Unix or Linux A pipe is a form of redirection (transfer of standard output to some other destination) that is used in Linux and other Unix-like operating systems to send the output of one command/program/process to another command/program/process for further processing. The Unix/Linux systems allow the stdout of
7 min read
pv command in Linux with Examples The 'pv' command, short for Pipe Viewer, is a terminal-based tool in Linux used to monitor data flow through pipes. It provides a visual representation of the progress, making it easier to track the status of long-running data transfers. The 'pv' command is particularly useful for monitoring data be
4 min read