You have 2 free member-only stories left this month.
5 Ways To Find a String Inside a File on Linux
Without any GUI tools of course
On your browser, to find a specific string on a webpage, all you need to know is the “CTRL+F” trick.
But as a backend or DevOps engineer, you have to handle something only on a Linux terminal instead of GUI tools. Life is not fair, is it?
But don’t worry, if you really master Linux skills, you will agree with me that using commands is more efficient than any GUI tool. And another bonus is it can make you become cool (Imagine a hacker in a fantasy movie who uses terminals all the time).
This article will introduce 5 convenient and efficient ways to find a string in a file on your Linux terminal.
Basically, there are two possible situations when you need to find a string inside a text file:
- You haven’t opened the file yet.
- You opened the file already.
For the first situation, we can use the famous three text processing weapons on Linux: grep
, sed
, and awk
.
For the second situation, we can leverage the power of the less
or vi
.
Before diving into details, let’s use the draft version of my poetry “The Zen of Writing on Medium”, which is a file named zen_of_medium.txt
, as the example file for the 5 ways.
1. Using grep Command
The grep
command is an intuitive and easy-to-use tool for string searching. Its basic structure is just grep string file_name
.
For example, if we would like to find where is the word “better” inside the zen_of_medium.txt
file. The command and results are as follows:
The above grep
command prints all the lines that contain the word “better”.
However, if it’s really a long-text file, finding the exact location of the results through the above approach is still hard. Fortunately, there is a -n
option in grep
which can print the line numbers of every line of result:
2. Using sed Command
The sed
command is another powerful tool for text processing. For our task, we can use it as follows:
Of course, the sed
command can do further, such as deleting or replacing the string after finding it. Those are beyond the scope of this article.
3. Using awk Scripting
There is one command in Linux which is too powerful to be called a command — awk. It’s literally a scripting language for text handling in Linux.
— Yang (“8 Levels of Using awk in Linux”)
Based on the above saying, you can imagine that the awk
language can be used to find strings as well.
Now, we’ll follow the syntax of the awk
:
awk ‘pattern {action}’ filename
In our case, the pattern is just the word “better”, and the action is to print out the line numbers and content of every matched line.
As shown above, the awk
command gives us the exactly correct results as the grep
and sed
commands.
4. Checking the File with less
If a file’s content has been displayed on the terminal already using the less
command, we can just use the tricks of less
.
To search characters with less
, type /
followed by the string we want to search for, and then press “Enter”.
If there are many matched words, we can press n
to find the next occurrence or N
to find the previous occurrence.
5. Checking and Editing the File with vi
Sometimes, we need to edit the characters after finding them in the file. In this case, the vi
editor is a good choice.
And what makes our life easier is the syntax of vi
for finding a string is the same with less
.
- Type
/
followed by the string, and then press “Enter”. - Press
n
to find the next occurrence orN
to find the previous occurrence.
Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles. 🙂
More articles about Linux and DevOps: