16. grep command - muneeb-mbytes/linux_course GitHub Wiki

GREP COMMAND

The figure.1. represents the different variation of grep command.

grep-Page-4 drawio

                  Figure.1. Different variation of grep command

Grep is a commonly used command in Linux (or Unix) to search through 1 or more files for a pattern or word.

'GREP' Stands for "global regular expression print". The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression.

Syntax: grep [options] pattern [files]

S.no grep commads Description
1. grep searches for a string in one file
2. grep -i searches for string with case-insensitive
3. grep -c Gives the count of string in that file
4. grep -l Gives list of files names only
5. grep -o print only matched part of string
6. grep -n prints string along with line number
7. grep -v prints the lines except the string is present
8. grep -r checks the string in entire directory
9. grep -e searches for multiple strings in one file
9. grep -E searches for multiple strings in one file with advanced regular expressions

grep command with no argument -

This command is use to find the string or a particular pattern in one file.

Syntax : grep string file_name

The Figure.2. shows how to find strings in one or more than files.

grep 3 1 drawio

                    Figure.2. Usage of grep command

grep

                                           GIF.1. grep output GIF

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep.csh


There are different options of grep command and these are -

grep -i

The -i flag is shorthand for --ignore-case. This command is use for case insensitive search . The -i option enables to search for a string case insensitively in the given file.

Syntax : grep -i string File_name

The Figure.3. shows how to use grep -i command for case sensitive search.

Untitled Diagram-Page-15 drawio

                    Figure.3. Usage of grep -i command

grep -i1

                                           GIF.2. grep -i output GIF

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_i.csh


grep -c

This command is use to displaying the count of number of matches of the given string or pattern .

Syntax : grep -c string <file_name>

The Figure.4. displaying the count of number of matches .

Untitled Diagram-Page-13 drawio

                 Figure.4. Usage of grep -c command

grep_c

                                           GIF.3. grep -c output GIF

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_c.csh


grep -l

This command is use to display the file name in which the string or pattern matches . It just display the file name .

Syntax : grep -l string *

             OR 

        grep -l string file_name file_name file_name file_name  (list all the file name in which we want to check the string )

The Figure.5. display the file in which string is matches.

grep -l drawio

                  Figure.5. Usage of grep -l command

grep -l

                                           GIF.4. grep -l output GIF

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_l.csh


grep -o

This command only display the matched string or pattern in file . It gives output , the only matched string or pattern .

Syntax : grep -o string file_name

The Figure.6. shows the matched string or pattern in a file.

Untitled Diagram-Page-18 drawio

                   Figure.6. Usage of grep -o command

grep -o

                                           GIF.5. grep -o output GIF

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_o.csh


grep -n

This command is use to show the line number of file with the line matched string or pattern.

Syntax : grep -n string file_name

The Figure.7. shows the line number of file with the line matched string.

grep 1 4 drawio

                      Figure.7. Usage of grep -n command

grep -n

                                           GIF.6. grep -n output GIF   

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_n.csh


grep -v

This command is use to display the lines that are not matched with the specified search string/pattern in the file . This command use the -v flag along with grep to invert the search. The -v flag is shorthand for --invert-match. This means the search will return all non-matching lines in a file the pattern you are searching for.

Syntax : grep -v string file_name

The Figure.8. shows the lines that are not matched with specified search string.

grep-Page-11 drawio (1)

                    Figure.8. Usage of grep -v command

grep -v

                                           GIF.7. grep -v output GIF

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_v.csh


grep -r

This command is use when you want to search through multiple files or you are unsure what file contains a pattern you can search recursively through a directory with grep and the -r flag. The -r flag is shorthand for --recursive.

The command will use grep, the -r flag, the pattern/string you want to search for, and the path of the directory to search in. This command will search the string/pattern in each file of the given directory .

**Syntax **: grep -r path/directory

The Figure.9. shows it will search recursively through the directory with command.

grep -r9 drawio

                   Figure.9. Usage of grep -r command

grep -r

                                           GIF.8. grep -r output GIF 

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_r.csh


grep -e

This command is use to search for different pattern/string in more than one file in one step . grep command specifies expression with -e option .

Syntax : grep -e string -e string -e string file_name

The Figure.10. display how to search for different pattern in more than one file.

grep -e0 drawio

                   Figure.10. Usage of grep -e command

grep -e

                                           GIF.9. grep -e output GIF 

Github lab link : https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_SiliconCrew/grep_commands/grep_e.csh

grep -E

Used for searching for patterns in a file with advanced Regular expressions. grep -E '^(VERSION|NAME)=' /etc/os-release

**Output: **

image

Difference between grep -e and grep -E

The grep -e and grep -E options are both used to search for lines in a file that match a specific pattern. However, there is a subtle difference between the two options.

grep -e uses basic regular expressions (BRE), which are a more limited subset of regular expressions. BRE characters have specific meanings, such as . matching any character and * matching zero or more occurrences of the preceding character. To use a BRE character literally, you must escape it with a backslash .

grep -E uses extended regular expressions (ERE), which are a more powerful and flexible subset of regular expressions. ERE characters do not have specific meanings by default, so they are matched literally unless they are escaped. This makes ERE more suitable for complex searches that involve more advanced patterns.

In general, you should use grep -E unless you specifically need the features of BRE. For example, if you are trying to match a pattern that contains a literal . or *, you will need to escape those characters with \ when using grep -e.

grep --help

This command is use to learn more on Grep command usage . By running this command , it will show all the flag options with their uses.

Syntax : grep --help


To learn vimdiff command: https://github.com/muneeb-mbytes/linux_course/wiki/17.-vimdiff

⚠️ **GitHub.com Fallback** ⚠️