Pipes and Filters in Unix / Linux
Pipes and filters in Unix 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.
In this article, we will cover below topics:
Pipes in Unix
Redirecting input/output to a named file, two commands can be connected together so that the output from one program becomes the input of the next program using pipe. When a pipe is established between two commands, the output of first command is redirected to standard input of second command. You can achieve this by using a vertical bar (|) on the command line between two commands represents pipe.
SYNTAX: $command 1|command 2
Filters in Unix
Any program that can take input from standard input and write results to standard output (after processing) is called filter.
The stdin and stdout can be managed as per preferences using redirection and pipes. Common filter commands are: grep, sort , more etc.
It is a pattern or expression searching command.
Prints found matches.
$grep[options] "pattern to be matched" [filename ]
$grep 'hello' testFile.txt
Output : searches hello in the testFile.txt and outputs/returns the lines containing ‘hello’.
Grep command can also be used with meta-characters:
$grep 'test' *
Output : it searches for ‘test’ in all the files and directories.
* is a meta-character and returns matching 0 or more preceding characters
refer here for more examples on grep command.
- Ordered arrangement.
- sorts lines of text alphabetically or numerically, default sorting is alphabetical
SYNTAX: $sort[options] filename
-d : dictionary order sort
-n : arithmetic/numeric order
-r : reverse order
-k : n Sorts on field n
-b : ignore leading blanks
-m: Merge (not sort)
$sort file1.txt $sort -n file2.txt
- customize the display content of the file.
- It displays the text file contents on the terminal with paging controls.
You can use below key controls with more command:
- You can display next line by pressing the enter key
- To bring up next screen, press spacebar
- If you want to move to the next file, press n
- To quit, press q.
SYNTAX: $more[options] filename
$cat file1.txt | more
After executing above command, more command is displayed at the bottom of the screen. Use keyboard inputs to move through the text.