What is grep command in linux

What is the grep command in Linux?

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

The grep command is part of the GNU Core-utilities package and filters text files for a given character arrangement or pattern.

The GNU Core-utilities package is available on all Unix-like operating systems.

Arguments

The grep command supports many functionalities. Users can select a functionality of their choice by setting the right flags or arguments. In this shot, we go through the most frequently used arguments for grep .

grep -c “pattern” file.txt

The -c flag is used to output the number of lines in file.txt that contain the word pattern as a string or sub-string.

grep -i “paTTern” file.txt

The -i flag makes sure that grep performs a case-insensitive search. It displays all the lines in file.txt that contain the word paTTern , regardless of the alphabetical case as a string or sub-string.

grep -l “pattern” *

The -l flag outputs the name of all those files containing text with the word pattern in it as a string or sub-string.

grep -w “pattern” file.txt

The -w flag is used to output all those lines in file.txt containing pattern as a whole word and not as a sub-string.

Читайте также:  X11 programming in linux

grep -o “pattern” file.txt

The o flag displays only the matched pattern instead of displaying the entire string or line which contains it.

grep -n “pattern” file.txt

The -n flag outputs the line number of the lines containing pattern alongside the line itself.

grep -v “pattern” file.txt

The -v flags invert the search results, as it displays those strings in file.txt which do not contain pattern .

grep “^pattern” file.txt

The ^ operator is used to output all those lines in file.txt which have pattern as their first word.

grep —help

The —help flag is used to open the manual page of grep , which contains additional information about it.

Example

In the example below, we use the echo command to create a text file, and write Educative! , Edpresso , and Sadzub on lines 1, 2, and 3, respectively.

Subsequently, we use the -i flag to run a case-insensitive search for the sub-string EDUCative in our text file and print the string which contains it. Additionally, we use the -n flag to print the line number of the line, which contains EDUcative and zub .

Searching for Zub would not yield any results because the -n flag runs a case-sensitive search.

Last but not least, we use the -o flag to print the matched pattern itself, i.e., Educative .

Note that for the -o flag, there is no exclamation mark at the end of the output.

Источник

Grep Command in Linux – Usage, Options, and Syntax Examples

Zaira Hira

Zaira Hira

Grep Command in Linux – Usage, Options, and Syntax Examples

Grep is a useful command to search for matching patterns in a file. grep is short for «global regular expression print».

If you are a system admin who needs to scrape through log files or a developer trying to find certain occurrences in the code file, then grep is a powerful command to use.

In this article, we will discuss the grep command’s syntax and its usage with some examples.

Syntax of the grep Command

The syntax of the grep command is as follows:

Читайте также:  What is avahi daemon in linux

image-73

How to Ignore Case Distinctions using -i

We can command grep to return results while ignoring the case of the matching string. Let’s find the word «berries» from our sample file. It should match all occurrences of «berries» regardless of their case.

image-74

How to Select the Non-Matching Lines using -v

We can reverse the results of the grep command to include non-matching results. Let’s say, if we want to get all the lines that do not contain the word «berries», the command would look like this:

image-75

The output returned all the lines that do not contain «berries».

How to Find the Line Numbers Against Matching Input using -n

There are times when we want to get the line numbers against the matching string. For that, we can supply the -n flag to grep like this:

image-76

From the output, we can see that the word «berries» occurs on line #5 of the file.

How to Find the Exact Matching Word from the Input File or String using -w

If you were to match an exact word rather than just the occurrence of a pattern, you can do so by using the -w flag.

If we grep «fruit» without any flags, it would return any occurrence of the word «fruit». This would include occurrences like «grapefruit«, «dragonfruit» and so on like this:

image-77

But, if we only want «fruit» in our results, we can use the -w flag like this:

image-78

Do you see the difference? The latter result returned only one line where the exact word «fruit» was found.

How to Count the Number of Occurrences of the Provided Pattern using -c

We can count the number of times the matched string appears in the file. This is how the -c flag works:

image-79

The word «fruit» appears 3 times in the file.

Tip: The -c flag can be very useful if you have to count the number of times an error message appeared in a log file.

Читайте также:  Роса линукс установка deb

How to Scan Files for Matching Input

Until now we had discussed how to search for matching patterns in a file or input string. We can also use grep to narrow down the files that contain a matching pattern. We can use the following flags to separate files that contain the matching pattern:

  • -l, —files-with-matches : Prints the file name that contains the provided matching pattern.
  • -L, —files-without-match : Prints the file name that does not contain the provided matching pattern.

Let’s say we have the following files in our folder:

zaira@Zaira:~/grep-tutorial$ ls -lrt total 12 -rw-r--r-- 1 zaira zaira 327 Jan 16 19:25 fruits.txt -rw-r--r-- 1 zaira zaira 47 Jan 16 20:31 vegetables.txt -rw-r--r-- 1 zaira zaira 194 Jan 16 20:33 more-fruits.txt

The files have the following content:

zaira@Zaira:~/grep-tutorial$ cat fruits.txt apples and pears citrus – oranges, grapefruits, mandarins and limes stone fruit – nectarines, apricots, peaches and plums tropical and exotic – bananas and mangoes berries – strawBERRIES, raspberries, blueberries, kiwifruit and passionfruit melons – watermelons, rockmelons and honeydew melons tomatoes and avocados.
zaira@Zaira:~/grep-tutorial$ cat more-fruits.txt passionfruit dragon fruit kiwis pears grapefruits, mandarins and limes stone fruit – nectarines, apricots, peaches and plums tropical and exotic – bananas and mangoes tomatoes and avocados.
zaira@Zaira:~/grep-tutorial$ cat vegetables.txt # Vegetables only Cabbage Onion Carrot Potato

Next, we want to see files that contain the pattern «fruit». We can do that like this:

Note that the * searches for all the files in the folder.

zaira@Zaira:~/grep-tutorial$ grep -l "fruit" * fruits.txt more-fruits.txt

Let’s say we want to list files that do not contain the word «fruit». We can achieve that using the command below:

zaira@Zaira:~/grep-tutorial$ grep -L "fruit" * vegetables.txt

Wrapping Up

If you want to study the grep command in depth, have a look at the man pages. You can get additional information on the command line with grep —help .

I hope you found this tutorial helpful!

What’s your favorite thing you learned from this tutorial? Let me know on Twitter!

You can read my other posts here.

Источник

Оцените статью
Adblock
detector