# Filter (software)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Filter_(software)
> Markdown URL: https://mediated.wiki/source/Filter_(software).md
> Source: https://en.wikipedia.org/wiki/Filter_(software)
> Source revision: 1345681944
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Computer program to process a stream}}
{{for multi|Internet filtering software|Content-control software|video filtering software|Filter (video)|other uses|Email filtering}}
A '''filter''' is a [computer program](/source/computer_program) or [subroutine](/source/subroutine) to process a [stream](/source/Stream_(computing)), producing another stream. While a single filter can be used individually, they are frequently strung together to form a [pipeline](/source/Pipeline_(software)).

Some [operating system](/source/operating_system)s such as [Unix](/source/Unix) are rich with filter programs. [Windows 7](/source/Windows_7) and later are also rich with filters, as they include [Windows PowerShell](/source/Windows_PowerShell). In comparison, however, few filters are built into [cmd.exe](/source/cmd.exe) (the original [command-line interface](/source/command-line_interface) of Windows), most of which have significant enhancements relative to the similar filter commands that were available in [MS-DOS](/source/MS-DOS). [OS X](/source/OS_X) includes filters from its underlying Unix base but also has [Automator](/source/Automator_(software)), which allows filters (known as "Actions") to be strung together to form a pipeline.

==Unix==
In [Unix](/source/Unix) and [Unix-like](/source/Unix-like) operating systems, a filter is a program that gets most of its data from its [standard input](/source/standard_input) (the main input stream) and writes its main results to its [standard output](/source/standard_output) (the main output stream). Auxiliary input may come from command line flags or configuration files, while auxiliary output may go to [standard error](/source/standard_error_stream). The command syntax for getting data from a device or file other than standard input is the input operator (<code>&lt;</code>). Similarly, to send data to a device or file other than standard output is the output operator (<code>&gt;</code>). To append data lines to an existing output file, one can use the append operator (<code>&gt;&gt;</code>). Filters may be strung together into a [pipeline](/source/Pipeline_(software)) with the pipe operator ("<code>|</code>"). This operator signifies that the main output of the command to the left is passed as main input to the command on the right.

The [Unix philosophy](/source/Unix_philosophy) encourages combining small, discrete tools to accomplish larger tasks. The classic filter in Unix is [Ken Thompson](/source/Ken_Thompson)'s {{Mono|[grep](/source/grep)}}, which [Doug McIlroy](/source/Doug_McIlroy) cites as what "ingrained the tools outlook irrevocably" in the operating system, with later tools imitating it.<ref name="reader">{{cite tech report |first1=M. D. |last1=McIlroy |authorlink1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=CSTR |number=139 |institution=Bell Labs}}</ref> {{Mono|grep}} at its simplest prints any lines containing a character string to its output. The following is an example:
<syntaxhighlight lang="bash">
cut -d : -f 1 /etc/passwd | grep foo
</syntaxhighlight>
This finds all registered users that have "[foo](/source/foo)" as part of their username by using the [cut](/source/Cut_(Unix)) command to take the first field (username) of each line of the Unix system password file and passing them all as input to grep, which searches its input for lines containing the character string "foo" and prints them on its output.

Common Unix filter programs are: [cat](/source/Cat_(Unix)), [cut](/source/cut_(Unix)), [grep](/source/grep), [head](/source/head_(Unix)), [sort](/source/Sort_(Unix)), [tail](/source/tail_(Unix)), and [uniq](/source/uniq).
Programs like [awk](/source/awk) and [sed](/source/sed) can be used to build quite complex filters because they are fully programmable. Unix filters can also be used by [Data scientists](/source/Data_scientists) to get a quick overview about a file based dataset.<ref>[http://blog.comsysto.com/2013/04/25/data-analysis-with-the-unix-shell/ Data Analysis with the Unix Shell] {{Webarchive|url=https://web.archive.org/web/20160122040316/http://blog.comsysto.com/2013/04/25/data-analysis-with-the-unix-shell/ |date=2016-01-22 }} - Bernd Zuther, comSysto GmbH, 2013</ref>

===List of Unix filter programs===
{{Div col|colwidth=15em}}
*[awk](/source/AWK)
*[cat](/source/cat_(Unix))
*[comm](/source/comm)
*[compress](/source/compress)
*[cut](/source/cut_(Unix))
*[expand](/source/Expand_(Unix))
*[fold](/source/Fold_(Unix))
*[grep](/source/grep)
*[head](/source/head_(Unix))
*[nl](/source/Nl_(Unix))
*[paste](/source/paste_(Unix))
*[perl](/source/perl)
*[pr](/source/Pr_(Unix))
*[sed](/source/sed)
*[sh](/source/Unix_shell)
*[sort](/source/Sort_(Unix))
*[split](/source/Split_(Unix))
*[strings](/source/Strings_(Unix))
*[tac](/source/Tac_(Unix))
*[tail](/source/tail_(Unix))
*[tee](/source/Tee_(command))
*[tr](/source/tr_(Unix))
*[uniq](/source/uniq)
*[wc](/source/Wc_(Unix))
*[zcat](/source/gzip)
{{Div col end}}

==DOS==
Two standard filters from the early days of DOS-based computers are [find](/source/List_of_DOS_commands) and [sort](/source/List_of_DOS_commands).

Examples:

 find "keyword" < ''inputfilename'' > ''outputfilename''
 sort "keyword" < ''inputfilename'' > ''outputfilename''
 find /v "keyword" < ''inputfilename'' | sort > ''outputfilename''

Such filters may be used in [batch files](/source/batch_files) (*.bat, *.cmd etc.).

For use in the same [command shell](/source/command_shell) environment, there are many more filters available than those built into Windows.  Some of these are [freeware](/source/freeware), some [shareware](/source/shareware) and some are commercial programs.  A number of these mimic the function and features of the filters in Unix. Some filtering programs have a [graphical user interface](/source/graphical_user_interface) (GUI) to enable users to design a customized filter to suit their special [data processing](/source/data_processing) and/or [data mining](/source/data_mining) requirements.

==Windows==
[Windows Command Prompt](/source/Windows_Command_Prompt) inherited MS-DOS commands, improved some and added a few. For example, [Windows Server 2003](/source/Windows_Server_2003) features six command-line filters for modifying [Active Directory](/source/Active_Directory) that can be chained by piping: DSAdd, DSGet, DSMod, DSMove, DSRm and DSQuery.<ref>{{cite book|last1=Holme|first1=Dan|last2=Thomas|first2=Orin|title=Managing and maintaining a Microsoft Windows Server 2003 environment : exam 70-290|date=2004|publisher=[Microsoft Press](/source/Microsoft_Press)|location=[Redmond, WA](/source/Redmond%2C_WA)|isbn=9780735614376|pages=[https://archive.org/details/mcsaselfpacedtra00micr/page/3 3{{!}}17—3{{!}}26]|url=https://archive.org/details/mcsaselfpacedtra00micr/page/3}}</ref>

[Windows PowerShell](/source/Windows_PowerShell) adds an entire host of filters known as "cmdlets" which can be chained together with a pipe, except a few simple ones, e.g. <code>Clear-Screen</code>. The following example gets a list of files in the <code>C:\Windows</code> folder, gets the size of each and sorts the size in ascending order. It shows how three filters (<code>Get-ChildItem</code>, <code>ForEach-Object</code> and <code>Sort-Object</code>) are chained with pipes.
<syntaxhighlight lang="powershell">
Get-ChildItem C:\Windows | ForEach-Object { $_.length } | Sort-Object -Ascending
</syntaxhighlight>

==References==
{{Reflist}}

== External links ==
* http://www.webopedia.com/TERM/f/filter.html

{{Programming paradigms navbox}}

Category:Software design patterns
Category:Programming paradigms
Category:Operating system technology

---
Adapted from the Wikipedia article [Filter (software)](https://en.wikipedia.org/wiki/Filter_(software)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Filter_(software)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
