In the Linux command-line utilities, few tools are really has much power and versatility like “grep” command. In this article we’ll learn how to use grep command in Linux with simple examples.
Table of Contents
What is Grep Command In Linux With Example.
Grep command is a powerful tool in Linux which is used to search a string or pattern into the file.
Here we have described the grep command in Linux with some powerful question that make you to understand much easier.
After reading this post you will be able to understand when to use grep command.
1.how to search the string in multiple file
syntax: grep “string” filename
Example: grep “salary” *
* represents all file
2.how to display the line number in output
grep -n “string” filename
3.how to search string as regular expression
grep “fast.*host” filename
4.how to highlight the search result using grep
grep -color “string” filename
5.how to display the line excluding the pattern using -v
grep -v “string ” filename
6.how to display all line that start with specified pattern using ^
grep ^ “string ” filename
7.how to search pattern recursively
grep -r “pattern or string” filename
8.how to counting number of matches
grep -c “string” filename
9.how to search with case insensitive
grep -i “string” filename
10.how to search full word using grep command
grep -iw “wordname” filename
11.how to display N line after matches
grep -A “string” filename
12.how to display N line before matches
grep -B “string” filename
13.how to display N line around matches
grep -C “string” filename
14.how to show position of match in file
grep -o -b “pattern” filename -o match only string -b show position
15.how to display only filename which match string
grep -I “pattern” filename
Piping concept :
Piping concept are used when we want use two or more command at the same time.The symbol of pipe is ‘ | ‘ pipes enable Unix/Linux user to create a powerful command.
Example: while using cat command to display the file you can use other command such as less or more
cat filename | less
less command display the less content according to output screen
FAQ About Grep Command in Linux
What is grep command used for?
Grep command is used for searching a string or pattern in file. you may be required to scrap from a given file then you can use grep command.