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:
cat
cat // Displays the text of the file line by line.
Ex: cat file.txt
head
head // Displays the first n lines of the specified text files.
Ex: head file.txt
tail
tail // It works the same way as head, just in reverse order.
Ex: tail file.txt
sort
sort // Sorts the lines alphabetically by default.
Ex: sort file.txt
wc // wc command gives the number of lines, words and characters in the data.
Ex: wc file.txt
grep
grep // grep is used to search a particular information from a text file.
Ex: grep test file.txt
tac
tac // tac is just the reverse of cat.
Ex: tac file.txt
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’
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).