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

cat   // Displays the text of the file line by line.
Ex: cat file.txt
  1. head

head   // Displays the first n lines of the specified text files.
Ex: head file.txt
  1. tail

tail   // It works the same way as head, just in reverse order.
Ex: tail file.txt
  1. sort

sort   // Sorts the lines alphabetically by default.
Ex: sort file.txt
  1. uniq

uniq   // Removes duplicate lines.
Ex: uniq file.txt
  1. wc

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

grep   // grep is used to search a particular information from a text file. 
Ex: grep test file.txt
  1. tac

tac   // tac is just the reverse of cat.
Ex: tac file.txt
  1. sed

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.

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

  1. nl

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

Linux Filter commands accept input data from stdin (standard input) and produce output on stdout (standard output).

Last updated