The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by programmers, system administrators, and users alike for its efficiency and versatility in handling text data. In this article, we will explore the various aspects of the grep command.
Syntax of grep Command in Unix/Linux
The basic syntax of the `grep` command is as follows:
grep [options] pattern [files]
Here,
[options]: These are command-line flags that modify the behavior of grep.Â
[pattern]: This is the regular expression you want to search for.
[file]: This is the name of the file(s) you want to search within. You can specify multiple files for simultaneous searching.
Options Available in grep Command
|
Options
|
Description
|
|
-c
|
This prints only a count of the lines that match a pattern
|
|
-h
|
Display the matched lines, but do not display the filenames.
|
|
–i
|
Ignores, case for matching
|
|
-l
|
Displays list of a filenames only.
|
|
-n
|
Display the matched lines and their line numbers.
|
|
-v
|
This prints out all the lines that do not matches the pattern
|
|
-e exp
|
Specifies expression with this option. Can use multiple times.
|
|
-f file
|
Takes patterns from file, one per line.
|
|
-E
|
Treats pattern as an extended regular expression (ERE)
|
|
-w
|
Match whole word
|
|
-o
|
Print only the matched parts of a matching line, with each such part on a separate output line.
|
|
-A n
|
Prints searched line and nlines after the result.
|
|
-B n
|
Prints searched line and n line before the result.
|
|
-C n
|
Prints searched line and n lines after before the result.
|
Sample Commands
Consider the below file as an input.Â
cat > geekfile.txt
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Pratical Example of grep Command in Linux
1. Case insensitive search
The -i option enables to search for a string case insensitively in the given file. It matches the words like “UNIX”, “Unix”, “unix”.Â
grep -i "UNix" geekfile.txt
Output:Â

Case insensitive search
2. Displaying the Count of Number of Matches Using grep
We can find the number of lines that matches the given string/patternÂ
grep -c "unix" geekfile.txt
Output:Â

Displaying the count number of the matches
3. Display the File Names that Matches the Pattern Using grep
We can just display the files that contains the given string/pattern.Â
grep -l "unix" *
or
grep -l "unix" f1.txt f2.txt f3.xt f4.txt
Output:Â

The file name that matches the pattern
4. Checking for the Whole Words in a File Using grep
By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.Â
grep -w "unix" geekfile.txt
Output:Â

checking whole words in a file
5. Displaying only the matched pattern Using grep
By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.Â
grep -o "unix" geekfile.txt
Output:Â

Displaying only the matched pattern
6. Show Line Number While Displaying the Output Using grep -n
To show the line number of file with the line matched.Â
grep -n "unix" geekfile.txt
Output:Â

Show line number while displaying the output
7. Inverting the Pattern Match Using grep
You can display the lines that are not matched with the specified search string pattern using the -v option.Â
grep -v "unix" geekfile.txt
Output:Â

Inverting the pattern match
8. Matching the Lines that Start with a String Using grep
The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.Â
grep "^unix" geekfile.txt
Output:Â

Matching the lines that start with a string
9. Matching the Lines that End with a String Using grep
The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.Â
grep "os$" geekfile.txt
10.Specifies expression with -e option
Can use multiple times :Â
grep âe "Agarwal" âe "Aggarwal" âe "Agrawal" geekfile.txt
11. -f file option Takes patterns from file, one per line
cat pattern.txt
Agarwal
Aggarwal
Agrawal
grep âf pattern.txt geekfile.txt
12. Print n Specific Lines from a File Using grep
-A prints the searched line and n lines after the result, -B prints the searched line and n lines before the result, and -C prints the searched line and n lines after and before the result.Â
Syntax:
grep -A[NumberOfLines(n)] [search] [file]
grep -B[NumberOfLines(n)] [search] [file]
grep -C[NumberOfLines(n)] [search] [file]
Example:
grep -A1 learn geekfile.txt
Output: Â

Print n specific lines from a file
13. Search Recursively for a Pattern in the Directory
-R prints the searched pattern in the given directory recursively in all the files.
Syntax:
grep -R [Search] [directory]
 Example :
grep -iR geeks /home/geeks
Output:
./geeks2.txt:Well Hello Geeks
./geeks1.txt:I am a big time geek
----------------------------------
-i to search for a string case insensitively
-R to recursively check all the files in the directory.
Conclusion
In this article we discussed the grep command in Linux which is a powerful text-search tool that uses regular expressions to find patterns or text within files. It offers various options like case insensitivity, counting matches, and listing file names. With the ability to search recursively, use regular expression flags, and customize output, grep is a vital tool for Linux users to efficiently handle text-related tasks. Mastering grep enhances your ability to work with text data in the Linux environment.
grep command in Unix/Linux – FAQs
What is grep command on Linux?
grep searches the input files for lines containing a match to a given pattern list. When it finds a match, it outputs the line or can perform other specified actions. It’s incredibly useful for searching through large logs, files, or command outputs to find specific information.
How to run a grep command in Unix?
To run grep in Unix, you can use the following basic syntax:
grep [options] pattern [file...]
For example, to search for the word “error” in a file called log.txt, you would use:
grep "error" log.txt
Why grep is used in Unix?
grep is used in Unix for a variety of reasons, such as:
- Searching for specific patterns within files.
- Filtering output of other commands.
- Checking for the presence of certain data within databases or logs.
- Automating tasks in scripts that depend on the presence or absence of specified data.
What is the grep -R command in Unix?
The -R option in grep stands for recursive. When this option is used, grep will search through all directories and subdirectories starting from the given directory and perform the search operation. For example:
grep -R "search_term" /path/to/directory/
This command will search for “search_term” in all files located within the specified directory and all its subdirectories.
How to use grep to find a word?
To use grep to find a specific word in a file, you can simply pass the word as the pattern, and grep will print out every line in the file that contains that word. For instance:
grep "example" filename.txt
This will search for the word “example” in filename.txt and display all lines containing that word.
Additionally, if you want to ensure that grep matches only whole words, you can use the -w flag:
grep -w "example" filename.txt
Please Login to comment...