Table of Contents
This tip is about the how to Use grep Command on Linux. So read this free guide, How to Use grep Command on Linux step by step. If you have query related to same article you may contact us.
How to Use grep Command on Linux – Guide
Grep is an essential command for Linux and Unix. It is used to find text and strings in a specific file. In others words, the grep command searches the specified file for lines that match the strings or words. It is one of the most useful commands on Linux and Unix systems for developers and system administrators. We will see how to use grep on a Linux or Unix-like system. The grep utilities are a family that includes grep, egrep, and fgrep for searching files. For most use cases fgrep is sufficient because of its speed and it just looks for strings and words. However, entering grep is simple. So it’s a personal choice.
A line is not a line of text as it appears on the terminal screen. A line in a text file is a sequence of characters until a line break is inserted. The output of grep commands can contain entire paragraphs unless the search options are refined. The grep filter looks for a specific character pattern in a file and displays all lines that contain that pattern. The pattern searched in the file is called a regular expression (grep stands for global regular expression and expression search).
Grep Command Syntax:
Usage examples of ‘grep’
grep foo /file/name
Search the file /file/name for the word ‘foo’. Each match will be displayed on a separate line.
grep -i “foo” /file/name
The -i option can be useful Search /file/name for ‘foo’ ignoring the case of the word, i.e. foo Foo FOO etc.
grep ‘error 123’ /file/name
grep is not limited to looking for just a single words or strings. It can also search string sequences, including phrases. This is achieved by enclosing the string sequence that forms the pattern in quotes (single or double). The example above will look for the phrase ‘error 123’ in the file /file/name.
grep -r “192.168.1.5” /etc/
The grep search area can be expanded further using its -r option to recursively search an entire directory tree (ie a directory and all subdirectories within it). The above example searches all files in the /etc/ directory and all its subdirectories (including its subdirectories) for the string ‘192.168.1.5’
grep -w “foo” /file/name
When you search for foo, grep will match fooboo, foo123, etc. You can force grep to select only those lines that contain matches that form the whole words using the -w option.
egrep -w ‘word1|word2’ /file/name
Search and display 2 words in /file/name
grep -c test /file/name
The -c option causes grep to only report the number of times the pattern was matched for each file and not display the actual lines. The example above would show the total number of times the string “test” appears in the file /file/name.
grep –context=6 error /file/name.txt
Sometimes we are not only interested in the matching line, but also the lines around the matching lines, this is where the grep –context option comes in handy. It can be particularly useful to see what happens before or after any Error or Exception. In the example above, the –context option is used to print the 6 lines before and after a corresponding line with the word “error” in /file/name.txt
Final note
I hope you like the guide How to Use grep Command on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.