egrep Command in Linux

Learn via video courses
Topics Covered

Overview

The egrep command in Linux is a powerful tool used for searching and manipulating text based on patterns. It stands for 'Extended Global Regular Expressions Print', and as the name implies, it extends the capabilities of the original grep command by supporting additional regular expression syntax. It is commonly used for searching through text files and filtering output from other commands.

Syntax of egrep Command in Linux

The basic syntax of the egrep command in Linux is as follows:

Where:

  • options: This is where you specify any flags or options that change how the egrep command behaves.
  • pattern: This is the regular expression that egrep will search for. It must be enclosed in quotes.
  • file: This is the file(s) in which egrep will search for the pattern. If no file is specified, egrep reads from the standard input.

Options in egrep Command in Linux

  1. -i: This option makes the egrep command in Linux case-insensitive.

    For example -

    Output:

    This command searches for the word 'linux' in 'example.txt', regardless of case.

  2. -v: This option inverts the search, meaning it will return lines that do not match the pattern.

    For example -

    Output:

    This command returns lines from 'example.txt' that do not contain the word 'error'.

  3. -r: This option makes the search recursive, meaning it will also search in subdirectories.

    For example -

    Output:

    This command searches for the word 'main' in the current directory and all its subdirectories.

Example Usages

  • Search for a word in a file:

    Output:

    Explanation: This command searches for 'word' in 'example.txt' and prints the lines that contain it.

  • Count the number of lines that match a pattern:

    Output:

    Explanation: This command counts the number of lines in 'example.txt' that contain the word 'word'.

Tips

  • Use the -l option to print only the names of files that contain the pattern.

  • To search for multiple patterns, use the '|' character to separate them. For example: egrep 'pattern1|pattern2' file.txt

  • The '^' and '$' symbols can be used to match the start and end of a line, respectively.

Advanced Use Cases of egrep Command in Linux

  • Search for a pattern in the output of another command:

    Output:

    Explanation: This command lists all directories in the current location. The 'ls -l' command outputs a long listing of files and directories, and the egrep command filters out lines that don't start with 'd', which in 'ls -l' output indicates a directory.

  • Use grouping and quantifiers:

    Output:

    Explanation: This command searches for lines in 'example.txt' that contain the word 'error' or 'fail' repeated at least 2 times.

  • Match lines of a certain length:

    Output:

    Explanation: This command matches lines in 'example.txt' that are exactly 20 characters long. The '.' matches any character, and '{20}' specifies that the previous element must occur exactly 20 times.

Conclusion

  • The egrep command in Linux extends the functionality of the original grep command with additional regular expression syntax.

  • Common options for egrep include -i for case-insensitive search, -v to invert the search, and -r for recursive search.

  • Egrep can be used to search for patterns in the output of other commands, count lines that match a pattern, and much more.