Grep

What is Grep ? Support your answers wirth at least two grep commands.

Grep is an acronym that stands for Global Regular Expression Print.

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.

The grep command consists of three parts in its most basic form. The first part starts with grep, followed by the pattern that you are searching for. After the string comes the file name that the grep searches through.

The command can contain many options, pattern variations, and file names. Combine as many options as necessary to get the results you need. 

Most common grep commands with examples.

To Search a File

To print any line from a file that contains a specific pattern of characters, in our case phoenix in the file sample2, run the command:

grep phoenix sample2

Grep will display every line where there is a match for the word phoenix. When executing this command, you do not get exact matches. Instead, the terminal prints the lines with words containing the string of characters you entered.

To Search Multiple Files

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character.

In our case, the grep command to match the word phoenix in three files sample,sample2, and sample3 looks like this example:

grep phoenix sample sample2 sample3

 

Search All Files in Directory

To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command.

In this example, we use nix as a search criterion:

grep nix *

( The output shows the name of the file with nix and returns the entire line. )

To Find Whole Words Only

Grep allows you to find and print the results for whole words only. To search for the word phoenix in all files in the current directory, append –w to the grep command.

grep -w phoenix *

To Ignore Case in Grep Searches

As grep commands are case sensitive, one of the most useful operators for grep searches is -i. Instead of printing lowercase results only, the terminal displays both uppercase and lowercase results. The output includes lines with mixed case entries.

An example of this command:

grep -i phoenix *

Inverse grep Search

You can use grep to print all lines that do not match a specific pattern of characters. To invert the search, append -v to a grep command.

To exclude all lines that contain phoenix, enter:

grep -v phoenix sample

To Count the Number of Matches

Grep can display the filenames and the count of lines where it finds a match for your word.

Use the -c operator to count the number of matches:

grep -c phoenix

Leave Comment

Important Topics

Title
Unix
Features of Unix
Block Diagram of Unix System
Architecture of Unix Operating System
Linux Operating System
Linux Vs Unix
Unix vs Windows
Shell and types of Shell
Kernal
Advantages and disadvantages of Unix
Unix File System
Types of File
Process
Daemon Process
Process Life Cycle
Fork System Call
Grep
Piping in Unix
Users and types of Users
User Management Systems
SUDO Users
Basic Shell Commands in Linux