How to Search Text in file using Linux command ?

How to Search Text in file using Linux command

After the deployment of your web applications like flask web app e.t.c you must have noticed there is a file maintained for knowing the status of your running web app. That file is called the log file. In systems with the User Interface, you can easily read, scroll and search the text in the log file. But when it comes to the Linux terminal or the cloud server then it becomes difficult to basic tasks like searching, reading and finding something in the text file. Therefore in this tutorial of how-to, you will know How to Search Text in file using Linux command? Please read the entire tutorial for better remembering.

How to view Text Files or any File?

In this section, you will learn how you to view and edit the text or log file using the various Linux Commands.

Cat

This command is generally used to show the content of the text or the log file on the screen or to another tool. It means concatenate the output with the other command. Lets there is text_file.txt in my directory. I want to display all the content on the screen. then I will use the following command.

cat text_file.txt

cat display text file

You can also use the head text_file.txt and tail text_file.txt command to see the beginning of the 10 lines of the text and tail command for the last 10 lines. If you want to show some of the numbers of lines then you can use the -n option for the display the custom lines. For example
head -n3 text_file.txt will display three lines of text-only and in the same way for tail -n3 text_file.txt for the last 3 files.

head and tail with the custom number of lines

Output using the Pipeline

Pipeline is the use of one command output with the other command. Just in machine learning, you use the pipeline for the different steps you can use in the Linux also for the commands. For example, I want to display the line numbers with the text output. Then I will use the following command.

cat text_file.txt | cat -n 
cat text_file.txt | cant -n | tail -n2

Output

cat display text file using the pipeline

Less

It uses to paginated text and gives navigation controls to the text file such that you can easily scroll up and downs using the keyboard. Type the following command to get control of the text from the keyboard.

less text_file

How to Search for the text file?

You can also use the command for searching in the log file or finding some text in the file. Below some command-line tools for performing these tasks.

grep

It searches for the given string pattern or text in the file and print all the lines containing the strings. For example, you want to show all the lines of text containing the word “data”, then use the following command. If you see the output as permission denied then you will change the permission to see the file. You can read our post on changing the file permission. Also, the grep command is the case sensitive therefore you have use  -i option to tell the grep command to ignore the case. Run the following command.

grep -i “data” text_file

grep -ni command
You can also see the lines number for these words are using the -n command.

In the log file, there can be a lot of noise due to many outputs. Therefore grep is very useful for removing a lot of noisy text from the log file. You can use the -vi option with the grep to remove the line containing the matching term like below.

grep -n -vi "data" text_file.txt

remove the line for the matching string

Grep command is also very useful tools with regex. I am not going to the introduction of regex here. You can use -E option to tell the grep command that I am using the regex. For example, I want to output the line that contains more than six words then I will use the following command.

 

grep -E “\w{6,}” text_file

Output

grep with regex

That’s all for now. We hope you have found an interesting tutorial regarding searching text file in Linux Command. If you have any query then please contact us or message on our Facebook Page.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner