惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
罗磊的独立博客
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX

DigitalOcean Community Tutorials

Mastering grep with Regular Expressions for Efficient Text Search It's Time to Break Up with Your Cloud: Why AI Teams are Switching We Built a Private-Document AI App to Test Platform Security. Here Is What We Could Actually Verify. PostgreSQL Explained: A Complete Beginner-to-Advanced Guide How To Install and Configure Postfix on Ubuntu How To Build a Web Application Using Flask in Python 3 Build AI Reading List with DigitalOcean Functions and Mistral How To Concatenate Strings in Python How to Allow MySQL Remote Access Securely How To Install and Use Docker on Rocky Linux How To Build a Multi-Agent AI System with Docker Agent DSPy Use Cases: Build Optimized LLM Pipelines How To Submit AJAX Forms with jQuery Build an AI-Powered GPU Fleet Optimizer with the DigitalOcean AI Platform ADK Monitor GPU Utilization in Real Time: A Complete Guide Reduce File Size of Images in Linux - CLI and GUI methods Reduce PDF File Size in Linux: Tools and Methods How To Set Up a Private Docker Registry on Ubuntu How To Troubleshoot Terraform: Errors and Fixes How to Use Go Modules Python Multiprocessing Example: Process, Pool & Queue Convert Class Components to Functional Components with React Hooks How To Install and Configure Ansible on Ubuntu LLM Tokenizers Simplified: BPE, SentencePiece, and More How To Monitor System Authentication Logs on Ubuntu How to Use Traceroute and MTR to Diagnose Network Issues How to Deploy Postgres to Kubernetes Cluster Importing Packages in Go: A Complete Guide Create RAID Arrays with mdadm on Ubuntu How To Make an HTTP Server in Go
Mastering Grep command in Linux/Unix: A Beginner's T...
Anish Singh Walia · 2022-08-04 · via DigitalOcean Community Tutorials

What is Grep

Grep, short for “global regular expression print”, is a command used for searching and matching text patterns in files using regular expressions. Furthermore, the command comes pre-installed in every Linux distribution. In this guide, we will look at the most common grep command use-cases.

Key Takeaways:

  • Grep searches for patterns in files using regular expressions. The command comes pre-installed in every Linux distribution and prints lines that match the specified pattern.
  • Use -i to ignore case sensitivity when searching. This flag ensures your search matches text regardless of uppercase or lowercase variations.
  • The -r flag enables recursive searching across directories. This allows you to search for patterns in all files within the current directory and subdirectories.
  • The -v option inverts the output to show non-matching lines. This is useful when you want to filter out or exclude lines containing a specific pattern.
  • Combine -n to display line numbers where patterns are found. This helps you quickly locate matches within large files.
  • Use -w for exact word matching instead of partial pattern matching. This prevents false matches when your search term is part of a longer word.
  • Grep works with pipes to filter output from other commands. This makes it powerful for checking installed packages, analyzing logs, and processing command output.
  • Extended regex with -E enables advanced pattern matching with alternation. You can search for multiple patterns simultaneously using | (e.g., grep -E "error|warning|failure").

Grep Command in Linux

Grep command can be used to find or search a regular expression or a string in a text file. To demonstrate this, let’s create a text file welcome.txt and add some content as shown.

Welcome to Linux !
Linux is a free and opensource Operating system that is mostly used by
developers and in production servers for hosting crucial components such as web
and database servers. Linux has also made a name for itself in PCs.
Beginners looking to experiment with Linux can get started with friendlier linux
distributions such as Ubuntu, Mint, Fedora and Elementary OS.

Great! Now we are ready to perform a few grep commands and manipulate the output to get the desired results. To search for a string in a file, run the command below.

Syntax:

grep "string" filename

OR

filename grep "string"

Example:

grep "Linux" welcome.txt

Output:

grep command usage

As you can see, grep has not only searched and matched the string “Linux” but has also printed the lines in which the string appears. If the file is located in a different file path, be sure to specify the file path as shown below:

grep "string" /path/to/file

Colorizing Grep results using the --color option

If you are working on a system that doesn’t display the search string or pattern in a different color from the rest of the text, use the --color option to make your results stand out.

Example:

grep --color "free and opensource" welcome.txt 

Output:

grep command usage

Most Common use-cases of grep command

Searching for a string recursively in all directories

If you wish to search for a string in your current directory and all other subdirectories, search using the -r flag as shown:

grep -r "string-name" *

For example:

grep -r "linux" *

Output:

grep command usage example

Ignoring case sensitivity

In the above example, our search results gave us what we wanted because the string “Linux” was specified in uppercase and also exists in the file in uppercase. Now let’s try and search for the string in lowercase.

grep "linux" filename

Nothing from the output, right? This is because grep could not find and match the string “linux” since the first letter is lowercase. To ignore case sensitivity, use the -i flag and execute the command below:

grep -i "linux" welcome.txt

Output:

grep command usage examples

Awesome, isn’t it? The -i flag is normally used to display strings regardless of their case sensitivity.

Count the lines where strings are matched with -c option

To count the total number of lines where the string pattern appears or resides, execute the command below:

grep -c "Linux" welcome.txt

Output:

grep command usage examples

Using Grep to invert output

To invert the grep output, use the -v flag. The -v option instructs grep to print all lines that do not contain or match the expression. This means that instead of printing matching lines, grep does the opposite and prints all of the lines that don’t match the expression. Going back to our file, let us display the line numbers as shown. Hit ESC in the Vim editor, type a colon followed by:

set nu

Next, press Enter.

Output:

grep command usage examples

Now, to display the lines that don’t contain the string “Linux”, run:

grep -v "Linux" welcome.txt

Output:

grep command usage examples

As you can see, grep has displayed the lines that do not contain the search pattern.

Number the lines that contain the search pattern with -n option

To number the lines where the string pattern is matched, use the -n option as shown:

grep -n "Linux" welcome.txt

Output:

grep command usage examples

Search for exact matching word using the -w option

Passing the -w flag will search for the line containing the exact matching word as shown:

grep -w "opensource" welcome.txt

Output:

grep command usage examples

However, if you try:

grep -w "open" welcome.txt

No results will be returned because we are not searching for a pattern but an exact word!

Using pipes with grep

The grep command can be used together with pipes for getting distinct output. For example, if you want to know if a certain package is installed in an Ubuntu system, execute:

dpkg -L | grep "package-name"

For example, to find out if OpenSSH has been installed in your system, pipe the dpkg -l command to grep as shown:

dpkg -L | grep -i "openssh"

Output:

grep command usage

Displaying the number of lines before or after a search pattern

You can use the -A or -B flags to display the number of lines that either precede or come after the search string. The -A flag denotes the lines that come after the search string and -B prints the output that appears before the search string. For example:

ifconfig | grep -A 4 ens3

This command displays the line containing the string plus 4 lines of text after the ens3 string in the ifconfig command.

Output:

grep usage commands

Conversely, in the example below, the use of the -B flag will display the line containing the search string plus 4 lines of text before the ether string in the ifconfig command.

Example:

ifconfig | grep -B 4 ether

grep command usage

Using grep with REGEX - regular expressions

The term REGEX is an acronym for REGular EXpression. A REGEX is a sequence of characters that is used to match a pattern. Below are a few examples:

^      Matches characters at the beginning of a line
$      Matches characters at the end of a line
"."    Matches any character
[a-z]  Matches any characters between A and Z
[^ ..] Matches anything apart from what is contained in the brackets

Example:

To print lines beginning with a certain character, the syntax is:

grep ^character file_name

For instance, to display the lines that begin with the letter “d” in our welcome.txt file, we would execute:

grep ^d welcome.txt 

Output:

grep regex example

To display lines that end with the letter ‘x’, run:

grep x$ welcome.txt

Output:

grep regex end of line

Getting help with more Grep options

If you need to learn more about grep command usage, run the command below to get a sneak preview of other flags or options that you may use together with the command.

grep --help

Sample Output:

grep command usage

We appreciate your time for going through this tutorial. Feel free to try out the commands and let us know how it went.

Using grep with Advanced Regex

The grep command supports Extended Regular Expressions (ERE) using the -E flag, also known as egrep.

Example:

grep -E "error|warning|failure" logfile.txt

To search with groups and back-references, use:

grep -E "([0-9]{4})-([0-9]{2})-([0-9]{2})" dates.txt

For a deep dive into regex patterns, check out this tutorial on Using Grep Regular Expressions to Search for Text Patterns in Linux.

How grep Handles Multi-threading with GNU Implementation

The GNU grep implementation optimizes searches using multi-threading. If available, grep internally uses AVX2 vectorization to speed up pattern matching.

For multi-threaded searching on large files, you can use:

grep --mmap "pattern" largefile.txt

Or leverage parallel execution with xargs:

find /logs/ -type f | xargs -P 4 grep "error"

This allows grep to process multiple files in parallel, improving performance on multi-core systems.

Common Pitfalls with Recursive Searches in grep

When using grep -r, be mindful of:

  1. Symbolic Links Loops: Use grep -R to follow symbolic links safely.

  2. Binary File Output: Use grep --binary-files=text to process binaries as text.

  3. Permission Errors: Run grep with sudo for restricted directories.

Example of a safer recursive search:

grep -r --exclude-dir={proc,sys,dev} "error" /

FAQs

1. What is the grep command in Linux?

The grep (Global Regular Expression Print) command is a powerful text-searching utility in Linux/Unix that allows users to search for specific patterns in files or output streams. It supports basic and extended regular expressions to match complex text patterns efficiently.

For a more developer-friendly alternative, you can check out How to Install and Use Ack: A Grep Replacement for Developers on Ubuntu and Awk command in Linux.

2. How do you find text using grep in Linux?

To search for a word or phrase in a file, use:

grep "pattern" filename

For recursive searching in directories, use:

grep -r "pattern" directory/

To enhance searching with regular expressions, refer to Using Grep Regular Expressions to Search for Text Patterns in Linux.

3. How do I use grep in Linux?

Basic usage includes:

grep "text" file.txt

To ignore case sensitivity:

grep -i "text" file.txt

For advanced examples, you can check out this tutorial on Top 50+ Linux Commands You MUST Know.

4. How do I search all files in grep?

To search for a pattern in all files within a directory:

grep -r "pattern" /path/to/directory

To include hidden files:

grep -r --exclude-dir=".*" "pattern" /path/to/directory

Be cautious with recursive searches, as large directories can slow down the process.

5. How do I use grep to search recursively?

To search for a pattern across multiple files in subdirectories, use the -r flag:

grep -r "search_term" /directory

For better control, use the --include or --exclude options:

grep -r --include="*.log" "error" /var/logs/

One common pitfall with recursive searches is encountering symbolic links, which can be handled using the -R option instead of -r.

6. Comparison of grep and awk Commands in Linux

Feature grep awk
Primary Function Search for patterns in text Text processing and manipulation
Pattern Matching Supports basic and extended regular expressions Supports regular expressions and more advanced pattern matching
Output Control Limited output control Full control over output format and content
Data Manipulation No data manipulation capabilities Can perform arithmetic, string manipulation, and conditional statements
Complexity Simple and easy to use More complex and powerful, but steeper learning curve
Typical Use Cases Quick searches, filtering text Data extraction, transformation, and reporting

grep is mainly used for searching patterns in text, whereas awk is more powerful for text processing and can manipulate data based on conditions.

Using grep:

grep "error" logfile.txt

Using awk:

awk '/error/ {print $2, $3}' logfile.txt

7. How do I use regular expressions in grep?

grep supports regex for advanced searching:

grep -E "error|fail|warning" logfile.txt

Example:

  • ^text - Matches lines starting with “text”.
  • text$ - Matches lines ending with “text”.
  • [0-9] - Matches any digit.

For a detailed guide, refer to our tutorial on using Grep Regular Expressions to Search for Text Patterns in Linux.

8. Can grep be used in shell scripts?

Yes! grep is widely used in shell scripts for filtering output.

if grep -q "error" logfile.txt; then
    echo "Errors found in the log."
fi

For more automation tips, check out this tutorial on Top 50 Linux commands.