
The grep command in Linux is a sorting utility used to search for text patterns in files. It's also known as global regular expression print (grep) and actively matches patterns in output file types such as configurations, text, or application code.
This article explains how to use the grep command in Linux to search and manipulate text in files.
grep Command SyntaxBelow is the grep command syntax:
In the above command, pattern is the target text or regular expression to look up while [file...] specifies one or more files to search.
grep Command Optionsgrep CommandPerform a basic search.
The above command searches for the string Vultr in file.txt and displays all matching results in the file.
Output:
Ignore case sensitivity.
The above command looks up for the text Vultr and displays all results that match any case. For example, text such as vultr, vULTR, and VULTR match the search.
Output:
Invert match and display all lines that do not include a specific string.
The above command displays all lines that don't include the string Vultr.
Output:
Count lines with matching pattern.
The above command counts the number of lines that include the text Vultr.
Output:
List director files that match a specific filename order.
The above command lists all files in the working directory that include the string Hello.
Output:
Show line numbers.
The above command displays all lines that match the string Vultr and the respective line numbers.
Output:
Perform a recursive search.
The above command searches for the string Hello in all files within a specific directory and all sub-directories.
Output:
Match whole words.
The above command searches for the string Vultr as a whole word in the file file.txt.
Output:
grepThe grep command supports regular expressions (Regex) that describe a sets of strings to search with advanced pattern matching methods.
Search for a string at the beginning of a line.
The above command matches and displays all lines that start with the string Vultr.
Output:
Search for a string at the end of a line.
The above command matches and displays lines that end with the string rates..
Output:
Search for lines that containing a range of number from between 0 and 9.
The above command matches and displays the lines that contain any number in the specified range.
Output:
Search for multiple patterns.
The above command searches for all lines that contain either Agrawal or Aggrawal.
Output:
Combine grep with other commands to filter results.
The above command searches for all processes and uses grep to filter all running processes with the string process_name.
Output:
Use grep with find.
The above command searches for the string example in the find command output.
Output:
Display lines with context.
The above command displays three lines of context on each matching line.
Output:
Search compressed files.
The above command searches for the string pattern in the compressed file.gz file.
Output:
Exclude specific files from search.
The above command recursively searches for pattern but excludes files with the .log extension.
Output:
You have used the grep command to efficiently search and manipulate textual data in files and directories. For more command options, the man grep command to view the grep manual page.
0 Comments
Be the first to comment and share your perspective with the community.