In Linux and Unix Systems Grep, short for “global regular expression print”, is a command used in searching and matching text files contained in the regular expressions. Furthermore, the command comes pre-installed in every Linux distribution. In this guide, we will look at Common grep command usage with a few examples. Show
Grep Command in LinuxGrep 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.
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
OR
Example:
Output 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
Colorizing Grep results using the --color optionIf 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
Output Searching for a string recursively in all directoriesIf you wish to search for a string in your current directory and all other subdirectories, search using the
For example
Output Ignoring case sensitivityIn 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.
Nothing from the output, right? This is because grepping
could not find and match the string “linux” since the first letter is Lowercase. To ignore case sensitivity, use the
Output Awesome isn’t’ it? The- i is normally used to display strings regardless of their case sensitivity.Count the lines where strings are matched with -c optionTo count the total number of lines where the string pattern appears or resides, execute the command below
Output Using Grep to invert OutputTo invert the Grep output
, use the
Next, press Enter Output Now, to display the lines that don’t contain the string “Linux” run
Output 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 optionTo number the lines where the string pattern is matched , use the
Output Search for exact matching word using the -w optionPassing then
Output However, if you try
NO results will be returned because we are not searching for a pattern but an exact word! Using pipes with grepThe 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 Ubuntu system execute
For example, to find out if OpenSSH has been installed in your system pipe the
Output Displaying number of lines before or after a search pattern Using pipesYou can use the -A or -B to dislay 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
This command
displays the line containing the string plus 4 lines of text after the ens string in the ifconfig command. Output
Using grep with regual expressions (REGEX)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:
Example To print lines beginning with a certain character, the syntax is;
For instance, to display the lines that begin with the letter “d” in our welcome.txt file, we would execute
Output To display lines that end with the letter ‘x’ run
Output Getting help with more Grep optionsIf you need to learn more on 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.
Sample Output We appreciate your time for going through this tutorial. Feel free to try out the commands and let us know how it went.Which command is used to search pattern in files in Linux?grep stands for Globally Search For Regular Expression and Print out. It is a command line tool used in UNIX and Linux systems to search a specified pattern in a file or group of files.
Which command is used for searching a pattern in a file?The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in.
Which command in Linux is used for searching a pattern of text in files and displaying the results?Grep is a Linux / Unix command-line tool used to search for a string of characters in a specified file. The text search pattern is called a regular expression. When it finds a match, it prints the line with the result. The grep command is handy when searching through large log files.
What is grep in Linux command?In Linux and Unix Systems Grep, short for “global regular expression print”, is a command used in searching and matching text files contained in the regular expressions.
|