Linux is an open-source OS or Operating System. It has been used for a multitude of purposes, from programming, gaming, and creating complex systems that run simple functions easily. For decades now, Linux and its other kernel variants have gained huge popularity.
The main reason for using Linux and such systems has been the ability to customize. From basic coding to complex security systems, Linux is preferred by professionals who regularly code. Inherently, Microsoft and Apple do not provide the same level of control that Linux kernel systems provide.
Amidst growing security and data privacy concerns, Linux has become a primary option for many people. Most people use Linux as their main OS while running a Virtual machine for handling operations and functions only a window can, such as specific games.
Moving on with this present article will teach you 10 useful commands you can use for Filtering text. You can use these commands for performing effective file operations in Linux. You can essentially re-organize the way in which reports are generated.
You enter a command; a program or filter works based on that command. The filter then generates an output based on that command. Below are 10 such text filtering commands for effective file operation in Linux.
1. ‘Sed’ Command
Sed is the short form for Stream Editor. It is used for transforming basic text in an input Stream. It is quite a powerful tool for filtering text. The common syntax for the command is “Sed [path]”. You can perform quick and effective ‘search’ and ‘replace’ filter functions with this. For example, you can use it as “$sed ‘s/escape/mystery/’ room.txt. It will replace the words escape with mystery.
2. Cat Command
If you want to get a report of a file line by line, Cat is the command you use. It helps In quickly providing you with an output in the desired file. The common syntax for using a cat command is “Cat [Path]”. For example, Cat List_of_Escape_Rooms.txt will generate the text in the present file from each line.
3. Head Command
What do you do if you want to display just specific lines? Let’s say you want to display just the first 8 lines of a specified text file. You can use this code in such situations. Head command is used though the following syntax “head [-number_of_lines_to_print] [path]”. This way, you can get the display of n number of first lines. If you do not specify, the default is set to 10 lines. For example “$head list of escape rooms.txt.” will show the first 10 lines in the particular file.
4. Sort Command
Sort Command literally sorts the lines in the target text file. You can sort the text lines alphabetically as well as in many other creative ways. The Following syntax used for using this command is “sort [-options] [path]”. Make sure to check out other sort commands as well. For example “$short List_of_escape_rooms.txt.” will yield an output where all the escape rooms will be listed alphabetically.
5. Tail Command
So, we learned how head command helps in filtering the ‘first n lines of a target file.’ But it doesn’t do it from the bottom up. Thus, the Tail command helps you in filtering text from the bottom.
Thus, to filter the bottom n lines of a text file, you can use the tail command. The syntax for it is “tail [-number_of_lines_to_print] [path]”. There isn’t much difference between the ‘head’ command and ‘tail’ command. Apart from the order of the filter, the command works in the same way. For example “$tail list_of_escape_rooms.txt” will give the list from the bottom to up.
6. Uniq Command
A lot of files have duplicate lines in them; some of these lines are continuous. In order to filter them out, we use the Uniq command. Uniq command is a great tool that can help in detecting duplicate lines and then deleting them.
It helps in ensuring that unique lines are filtered when providing output. To use this command, you can use it through the following syntax “uniq [options] [path]”. Thus, if you are dealing with a file that has a lot of duplication, you can use this to ease your work.
As a limitation, this command (Uniq) only removes lines that are consecutive. Thus, if duplicate lines are not put together, the output will not remove them. For example “$uniq escaperooms.txt” will remove any duplicates adjacent to each other.
7. WC Command
Word Count or WC Command is a computing command. To use it as a filtering command, you can find out character count, word count, and the number of lines in a file.
The Syntax for wc command is “wc [-options] [path]”. This command produces an output that shows how many lines are there. It then follows it up by showing the word count. Then it shows the number of total characters. Finally, you get the file destination. It is all shown in one line but 4 different columns. For example “$wc List_of_escape_rooms.txt” will yield the number of lines, words, and characters. The output thus would be ‘10_90_300_’. Here 10 are the lines, 90 words, and 300 characters.
8. Grep Command
Grep Command or Globally search for regular expression and printout, is a useful tool for a number of things. Firstly, you can use the grep command to find a particular pattern of characters in a file. You can also use it to find lines that do not match a particular pattern.
The pattern that gets searched and filtered is known as the regular expression. The common syntax for grep command is “grep [options] pattern [files]”. For example “$grep crime List_of_escape_room.txt” will provide you lines where crime is used.
9. Tac Command
In order to get an output with all the lines in the reverse order, you cannot use the Cat command. You use the Tac command, which is conveniently the reverse of ‘Cat’. This command will display lines of a file in reverse order. Unless specified the last line will be the first to display and the rest will follow.
The syntax for this command is “tac [Path]” For example “$tac list_of_escape_rooms.txt” will provide all the lines from n to 1 in that file.
10. Nl Command
NI command is a simple but useful tool. Nl command effectively numbers all the lines in a file. For example, “$nl List_of_escape_room.txt” will filter and give output as:
1 Ayy Escape Room
2 Bee Escape room
3 Cee Escape room
And so on and so forth. Numbering each line consecutively.
Conclusion
With these 10 useful and simple commands, you will find your file operation to be easy and quick. These 10 text filters will help you complete a number of different functions and provide a number of different outputs. Depending on what you want, you can try each command for the most effective file operation.