Grep Command in Linux – Syntax and Examples Explained

Updated on September 2, 2024

Article Outline

On a VPS, finding files could initially appear to be a simple operation. Nevertheless, if you’re using a Linux OS without a GUI or have countless huge log files to review, it can easily become burdensome. The grep command in Linux is useful in this situation since it makes it simple for users to look for a string throughout the system. 

What is Grep Command in Linux?

“Global regular expression print” refers to the ‘grep’ command in Linux. The grep command filters a file’s content, which facilitates our search. A command-line tool is used to look for lines in plain-text data groups that match a regular expression. The command ‘ed,’ which has a similar effect, is where “grep” gets its name. Read a detailed guide on Linux Command and get your hands on the most important linux command today.

*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Basic Usage of Grep Command in Linux

It operates by looking for text and defined strings in a given file. In simple terms, grep Linux allows users to look for specific patterns or words in files and display any lines that have them. System administrators, for instance, use the grep command in Linux to look for specific lines in the countless number of systems and configuration files they manage. 

 

Make an SSH connection to the VPS to launch the grep command in Linux. Type ssh your-username@your-server. Linux users simply need to launch the terminal. 

Syntax and General Usage of the Grep Command in linux

The grep command in Linux has the following generic syntax: 

 

Grep [OPTION…] PATTERNS [FILE…]

 

Grep Linux looks for PATTERNS in each FILE using the syntax shown above. Grep finds each line that matches the given PATTERN. When the grep command in Linux is used in a shell command, it is best practice to close the PATTERN in quotes. 

 

Here is a list of general usage of the grep command in Linux: 

 

-i, –ignore-case Ignores case distinctions in input data and pattern analysis.
-n, –line-number Include the line number from the input file at the beginning of each line in the matching output. 
-c Count the number of times the given pattern appears.
-w Find the word that most closely matches the provided file or string. 
-v, –invert-match Identifies and selects the lines of the given input pattern that do not match. 

Searching for a Specific Pattern

Using the following grep command syntax, you can look for a particular pattern: 

 

Grep PATTERN [FILE]

 

  • The pattern you want to look for is PATTERN. 
  • The file you want to search for is called FILE. The Grep command in Linux will look through the standard input if a file is not supplied. 

For instance, the program below will look for the pattern “hello” in the text file myfile.txt

 

grep hello myfile.txt

 

The phrase “hello” will appear on all the lines in myfile.txt that are printed. 

 

Interested in the use of AI technology? Don’t know how? Start with the Expert System in Artificial Intelligence. Learn everything about AI’s expert system!

How to Install Grep in Linux?

Grep Linux is pre-installed on practically all Linux distributions. If not, use the following grep command in Linux terminal window: 

 

$ sudo apt-get install grep  

 

Grep Command in Linux with Pipe:

 

Typically, the grep command in Linux is used with pipe (|). The syntax is: 

 

  • command | grep <searchWord>  

One of the grep command examples, when used with pipe, is:

 

  • cat marks.txt | grep 9  

Grep Command in Linux Without Pipe:

 

One can also use the Grep command in Linux without a pipe. The syntax will be like this: 

 

  • grep <searchWord> <file name>  

One of the grep command examples, when used without pipe, is:

 

  • grep 9 marks.txt  

Once grep Linux is set up, you may confirm it by issuing the following grep command once more: 

 

  • grep –version

Also read about: Linux vs. Windows: The Major Difference Between Linux and Windows

Implementations of Grep Command in Linux

  • Busybox Grep Command: This is a simplified version of grep that is a part of the Busybox toolkit. It is an excellent option for compact systems with constrained resources. 
  • Pgrep Command: This command-line utility enables you to look for processes using their names or patterns. In some circumstances, it can be used instead of grep. 
  • GNU Grep Command: The most used, open-source, free-to-access version of grep in Linux is GNU grep. 
  • Acme Grep Command in Linux: It is a for-profit version of Grep that Acme Software Corporation offers. It has a few capabilities that the GNU version does not, like the ability to look for regular expressions in binary archives. 

Wondering whether to pursue a career in AI or data science? Still confused? Check the major differences between data science and artificial intelligence to simplify your decision-making! 

Case-Sensitive and Case-Insensitive Searches

Grep searches are, by definition, case-sensitive. The search pattern must, therefore, precisely match the case of the text in the file. For instance, the search term “hello” will only find results for the word if it is written entirely in lowercase characters. Use the -i or –ignore-case options in grep to conduct a case-insensitive search. 

Introduction to Regular Expressions and their Importance in Grep

Regular expressions in grep Linux are highly versatile and concise ways to match a “string of text.”. A single letter, phrase, sentence, or specific combination of characters might all be considered to be a “string of text” in this definition. 

 

Grep command in Linux matches a collection of related patterns using a different kind of expression than the shell’s wild cards, which implement a single expression to match comparable filenames. 

Using Metacharacters and Special Characters in Pattern Matching

One can use special characters and metacharacters to create strong pattern matches in grep Linux. For instance, the pattern shown below matches any line that begins with the word “hello” and ends with any quantity of digits: 

 

hellod+

 

This pattern matches any single character using the ‘.’ metacharacter, any group of characters using the ‘*’ metacharacter, and a digit using the ‘[]’ character. 

Examples of Regular Expressions for Advanced Searching

REGular EXpression is referred to as REGEX. A string of characters called a REGEX is used to match patterns. Here are a few Linux Grep command examples of regular expressions for advanced searching:

 

^ Matching the characters at the line’s beginning.
$ Characters at the end of a line are matched with $. 
“.” Any character in “.” is a match.
[a-z] Any character between A and Z is matched by [a-z]. 
[..] Anything other than what is in the brackets is matched by [..].

Grep Options and Flags

Here are some examples of options and flags in Grep command in linux.

 

-c Only the lines that match a pattern are printed with the -c option. 
-h Display only the lines that match; do not show the filenames.
-i Ignores case for matching with -i.
-I It only shows a list of filenames.
-n Show the lines that match along with their line numbers. 
-v When using -v, any lines that don’t match the pattern are printed out. 
-e exp With the -e exp option, an expression is specified. It can be used repeatedly.
-f Takes patterns from a file, one per line, with the -f option. 
-E The pattern is treated as an extended regular expression (ERE) with the suffix -E. 
-w Match the full word with -w.
-o Print only the portions of a matching line that have been matched, each on a distinct output line. 

Examples of Grep Command in Linux

Here are the following Linux grep command examples to know about: 

 

grep -i hola myfile.txt When case distinctions are irrelevant when looking for the pattern “hola.”
grep hola * When all files in the present directory must be searched for the pattern “hola.”
grep -v hello myfile.txt When reversing the match, ensure only lines devoid of the “hello” pattern are printed.

Conclusion

Hundreds of files and folders can be easily searched through with the grep tool. You can locate particular lines easily thanks to the variety of flags it has in abundance. Want to learn more about Linux and excel in the field of AI and ML? Get started with Hero Vired’s Artificial Intelligence and Machine Learning course today! 

FAQs
Yes, you can make grep case-insensitive by adding the -i or —ignore-case flag.
The fgrep command is distinct from grep and egrep in that it looks for a string rather than a pattern that meets an expression.
To discover all lines that meet both patterns, use the && operator. To find all lines that meet either pattern, use the | operator. To discover all lines that don't fit a pattern, use the -v option.
Grep can be used to search via files or to filter the output of other commands when combined with pipes or other programs.

Updated on September 2, 2024

Link

Upskill with expert articles

View all
Free courses curated for you
Basics of Python
icon
5 Hrs. duration
icon
Beginner level
icon
9 Modules
icon
Certification included
avatar
1800+ Learners
View
Essentials of Excel
icon
4 Hrs. duration
icon
Beginner level
icon
12 Modules
icon
Certification included
avatar
2200+ Learners
View
Basics of SQL
icon
12 Hrs. duration
icon
Beginner level
icon
12 Modules
icon
Certification included
avatar
2600+ Learners
View
next_arrow
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved