DEV Community

Cover image for How to search and find a specific keyword or term from the contents of a file in the Linux terminal?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to search and find a specific keyword or term from the contents of a file in the Linux terminal?

Originally posted here!

For searching and finding a specific keyword or a term from the contents of a file in the Linux terminal, you can run the cat command followed by the path to the file, then the | operator (aka pipe operator), then the grep command and finally the keyword you want to search for.

TL;DR

# Search and find the occurrence of a specific
# word from the contents of a file
cat <PATH_TO_FILE> | grep <YOUR_SEARCH_TERM>
Enter fullscreen mode Exit fullscreen mode

For example, let's say we have a file called myFile.txt which has some content like below,

Example Content

Wikipedia is a free content, multilingual online encyclopedia written
and maintained by a community of volunteer contributors through a
model of open collaboration, using a wiki-based editing system.
Enter fullscreen mode Exit fullscreen mode

Now, we want to find the occurrence of the word encyclopedia from the contents of the myFile.txt file. We can do that using the cat and grep commands like this,

# Search and find the occurrence of a specific
# word from the contents of a file
cat myFile.txt | grep encyclopedia
Enter fullscreen mode Exit fullscreen mode

The output of running the above command may look like this,

output of running the command to search and find specific keyword

As you can see that the word encyclopedia is highlighted in the output which shows us that it is present.

See the execution of the above command live in repl.it.

That's all 😃!

Feel free to share if you found this useful 😃.


Top comments (0)