Javatpoint Logo
Javatpoint Logo

Grep Command in Linux/Unix with Examples

The 'grep' command stands for "global regular expression print". grep command filters the content of a file which makes our search easy.

It is a command-line utility to search plain-text data groups for lines that are the same as a regular expression. The name "grep" comes from the command, i.e., ed, which contains the same effect. Originally, grep was designed for the Unix operating system, but it became available for every Unix-like system later and a few others like OS 9.

The grep filter finds a file for a specific character pattern and shows every line that includes that pattern. Characters should be in quotation marks if they occur in the pattern parameter with a special meaning for the shell. Usually, we must enclose the whole pattern in one quotation mark if the pattern parameter is not a common string.

In an expression like [a-z], the minus sign (-) cml describes a range based on the current collating order. A collating order may specify equivalent classes in character ranges for use. The grep command assumes stdin when no files are mentioned.

The pattern is called the regular expression that is found inside the file. The pattern is restricted regular expressions in the format of the egrep or ed command. The grep command applies a solid non-deterministic algorithm. It comes in handy when we need to filter large log files.

Brief History of grep

Before it was titled, grep was a confidential utility specified by Ken Thompson to find files for several patterns. Unknown to its existence, Doug Mcllroy asked Thompson to specify such a function. Replying that he would overnight think of such a utility. Thompson made improvements and corrected bogs for about one hour on his program known as "s" (or search). He presented the function to Mcllroy the next day, who said it was the same as he wanted. The account of Thompson may describe the belief that the grep command was specified overnight.

Thompson specified the initial version in PDP-11 assembly language to support Lee E. McMohan checking the Federalist Papers text to decide the individual paper's authorship. The ed text editor had support for regular expression but couldn't be used on such large text; Thompson copied that code to a standalone tool. He selected the name due to in ed; the grep command would print every line the same as the described pattern. First, grep was added in Version 4 Unix, saying that "it's generally cited as the prototypical software tool", Mcllroy approved grep with the "irrevocably ingraining" tools philosophy of Thompson in Unix.

Implementations of grep

A range of grep implementations is present in several software development environments and operating systems. Early versions included fgrep and egrep, introduced in the 7 version of Unix. The egrep version supports syntax for an extended regular expression included by Alfred Aho after the original regular expression implementation of Ken Thompson. The fgrep version finds any fixed string list with the Aho-Corasick string matching algorithm. These version binaries are available in modern systems, connecting to grep or calling grep a shell script using the correct flag added while generally deployed on the POSIX systems.

Other commands include the "grep" word to represent they are finding tools, commonly ones that depend on regular expression matches. For example, the utility, i.e., "pgrep", shows the processes whose titles are the same as provided regular expression.

  • Grep is the title of the built-in function that searches for the elements within a list that amuse a certain property in the Perl programming language.
  • Typically, this higher-order function is called "filter" or "where" in another language.
  • The command, i.e., pcregrep, is a grep implementation that utilizes the syntax for Perl regular expression. The same functionality can be added to the grep GNU version using the -P flag.
  • Also, grep ports (e.g., in GnuWin32 and Cygwin) execute upon Microsoft Windows. A few Windows versions provide the same findstr or qgrep command.
  • Also, the grep command is a part of the MSX-DOS2 Tools of ASCII for the 2 version of MSX-DOS.
  • The fgrep, egrep, and grep commands have been shipped to the IBM i OS.
    The Adobe InDesign software has GREP functions (since CS3 edition) in the change/find "GREP" tab dialog box and specified using CS4 in the "GREP styles" paragraph styles.

How to install grep in Linux?

Grep comes pre-installed in almost every distribution of Linux. However, in case, we can install it with the below command in the terminal window if it is missing from our system:

grep with pipe

The 'grep' command is generally used with pipe (|).

Syntax:

Example:


Linux Grep Filters1

Look at the above snapshot, grep command filters all the data containing '9'.

grep without pipe

It can be used without pipe also.

Syntax:

Example:


Linux Grep Filters2

Look at the above snapshot, grep command do the same work as earlier example but without pipe.

grep options

  • grep -vM: The 'grep -v' command displays lines not matching to the specified word.
  • Syntax:

    Example:


    Linux Grep Filters3

    Look at the above snapshot, command "grep -v 9 marks.txt" displays lines hwich don't contain our search word '9'.

  • grep -i: The 'grep -i' command filters output in a case-insensitive way.
  • Syntax:

    Example:


    Linux Grep Filters4

    Look at the above snapshot, command "grep -i red exm.txt" displays all lines containing 'red' whether in upper case or lower case.

  • grep -A/ grep -B/ grep -C

    grep -A command is used to display the line after the result.

    grep -B command is used to display the line before the result.

    grep -C command is used to display the line after and line before the result.

    You can use (A1, A2, A3.....)(B1, B2, B3....)(C1, C2, C3....) to display any number of lines.

  • Syntax:

    Example:


    Linux Grep Filters5

    Look at the above snapshot, command "grep -A1 yellow exm.txt" displays searched line with next succeeding line, command "grep -B1 yellow exm.txt" displays searched line with one preceding line and command "grep -C1 yellow exm.txt" displays searched line with one preceding and succeeding line.


Next TopicLinux comm





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA