Grep Command in Linux/Unix with ExamplesThe '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 grepBefore 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 grepA 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.
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: Look at the above snapshot, grep command filters all the data containing '9'. grep without pipeIt can be used without pipe also. Syntax: Example: Look at the above snapshot, grep command do the same work as earlier example but without pipe. grep options
Syntax: Example: Look at the above snapshot, command "grep -v 9 marks.txt" displays lines hwich don't contain our search word '9'. Syntax: Example: Look at the above snapshot, command "grep -i red exm.txt" displays all lines containing 'red' whether in upper case or lower case. Syntax: Example: 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
|