> For the complete documentation index, see [llms.txt](https://devops-3.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devops-3.gitbook.io/devops/docs/module-1-linux-introduction/filters.md).

# Filters

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&#x20;

```bash
cat   // Displays the text of the file line by line.
Ex: cat file.txt
```

2. head

<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>head   // Displays the first n lines of the specified text files.
</strong>Ex: head file.txt
</code></pre>

3. tail&#x20;

<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>tail   // It works the same way as head, just in reverse order.
</strong><strong>Ex: tail file.txt
</strong></code></pre>

4. sort

```bash
sort   // Sorts the lines alphabetically by default.
Ex: sort file.txt
```

5. uniq

```bash
uniq   // Removes duplicate lines.
Ex: uniq file.txt
```

6. wc

{% code overflow="wrap" fullWidth="false" %}

```bash
wc   // wc command gives the number of lines, words and characters in the data. 
Ex: wc file.txt
```

{% endcode %}

7. grep&#x20;

<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>grep   // grep is used to search a particular information from a text file. 
</strong><strong>Ex: grep test file.txt
</strong></code></pre>

8. tac

{% code overflow="wrap" %}

```bash
tac   // tac is just the reverse of cat.
Ex: tac file.txt
```

{% endcode %}

9. sed&#x20;

```bash
sed   // sed stands for stream editor.
Ex: sed 's/test/text/g' file.txt
```

It allows us to apply search and replace operation on our data effectively. *sed* is quite an advanced filter and all its options can be seen on its man page.&#x20;

The expression we have used above is very basic and is of the form **‘s/search/replace/g’**&#x20;

10. &#x20;nl

{% code overflow="wrap" %}

```bash
nl   // nl is used to number the lines of our text data.
Ex: nl file.txt
```

{% endcode %}

{% hint style="info" %}
Linux Filter commands accept input data from stdin (standard input) and produce output on stdout (standard output).
{% endhint %}
