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 : Displays the text of the file line by line.
2. head : Displays the first n lines of the specified text files. If the number of lines is not specified then by default prints first 10 lines.
head [-number_of_lines_to_print] [path]
3. tail : It works the same way as head, just in reverse order. The only difference in tail is, it returns the lines from bottom to up.
tail [-number_of_lines_to_print] [path]
4. sort : Sorts the lines alphabetically by default but there are many options available to modify the sorting mechanism. Be sure to check out the man page to see everything it can do.
sort [-options] [path]
5. uniq : Removes duplicate lines. uniq has a limitation that it can only remove continuous duplicate lines(although this can be fixed by the use of piping). Assuming we have the following data.
uniq [options] [path]
You can see that applying uniq doesn’t remove any duplicate lines, because uniq only removes duplicate lines which are together.
When applying uniq to sorted data, it removes the duplicate lines because, after sorting data, duplicate lines come together.
6. wc : wc command gives the number of lines, words and characters in the data.
wc [-options] [path]
In above image the wc gives 4 outputs as:
- number of lines
- number of words
- number of characters
7. grep : grep is used to search a particular information from a text file.
grep [options] pattern [path]
Below are the two ways in which we can implement grep.
8. tac : tac is just the reverse of cat and it works the same way, i.e., instead of printing from lines 1 through n, it prints lines n through 1. It is just reverse of cat command.
9. sed : sed stands for stream editor. 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’
In the above image, we can clearly see that Scooby is replaced by Scrapy.
10. nl : nl is used to number the lines of our text data.
nl [-options] [path]
It can clearly bee seen in the above image that the lines have been numbered
- How to find time taken by a command/program on Linux Shell?
- Terminal Mail and Linux Security Mail
- Linux Virtualization - Chroot Jail
- Linux Virtualization : Linux Containers (lxc)
- Linux Kernel Module Programming: Hello World Program
- Formatted text in Linux Terminal using Python
- Some useful Linux Hacks
- Daily life Linux Commands
- Basic Linux Commands for day to day life
- Initializing and Cache Mechanism in Linux Kernel
- How to add "graphics.h" C/C++ library to gcc compiler in Linux
- Working with Magic numbers in Linux
- Reset a lost Linux administrative password and Explanation
- dup() and dup2() Linux system call
- Encrypt/Decrypt Files in Linux using Ccrypt
- Linux File Hierarchy Structure
- mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
- Network configuration and trouble shooting commands in Linux
- Netstat command in Linux
- Program to show that Linux provides time sharing environment to processes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.