Grep is the frequently used command in Unix (or Linux). Most of us use grep just for finding the words in a file. The power of grep comes with using its options and regular expressions. You can analyze large sets of log files with the help of grep command.
Grep stands for Global search for Regular Expressions and Print.
The basic syntax of grep command is
grep [options] pattern [list of files]
Let see some practical examples on grep command.
1. Running the last executed grep command
This saves a lot of time if you are executing the same command again and again.
!grepThis displays the last executed grep command and also prints the result set of the command on the terminal.
2. Search for a string in a file
This is the basic usage of grep command. It searches for the given string in the specified file.
grep "Error" logfile.txtThis searches for the string "Error" in the log file and prints all the lines that has the word "Error".
3. Searching for a string in multiple files.
grep "string" file1 file2This is also the basic usage of the grep command. You can manually specify the list of files you want to search or you can specify a file pattern (use regular expressions) to search for.
grep "string" file_pattern
4. Case insensitive search
The -i option enables to search for a string case insensitively in the give file. It matches the words like "UNIX", "Unix", "unix".
grep -i "UNix" file.txt
5. Specifying the search string as a regular expression pattern.
grep "^[0-9].*" file.txtThis will search for the lines which starts with a number. Regular expressions is huge topic and I am not covering it here. This example is just for providing the usage of regular expressions.
6. Checking for the whole words in a file.
By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.
grep -w "world" file.txt
7. Displaying the lines before the match.
Some times, if you are searching for an error in a log file; it is always good to know the lines around the error lines to know the cause of the error.
grep -B 2 "Error" file.txtThis will prints the matched lines along with the two lines before the matched lines.
8. Displaying the lines after the match.
grep -A 3 "Error" file.txtThis will display the matched lines along with the three lines after the matched lines.
9. Displaying the lines around the match
grep -C 5 "Error" file.txtThis will display the matched lines and also five lines before and after the matched lines.
10. Searching for a sting in all files recursively
You can search for a string in all the files under the current directory and sub-directories with the help -r option.
grep -r "string" *
11. Inverting the pattern match
You can display the lines that are not matched with the specified search sting pattern using the -v option.
grep -v "string" file.txt
12. Displaying the non-empty lines
You can remove the blank lines using the grep command.
grep -v "^$" file.txt
13. Displaying the count of number of matches.
We can find the number of lines that matches the given string/pattern
grep -c "sting" file.txt
14. Display the file names that matches the pattern.
We can just display the files that contains the given string/pattern.
grep -l "string" *
15. Display the file names that do not contain the pattern.
We can display the files which do not contain the matched string/pattern.
grep -L "string" *
16. Displaying only the matched pattern.
By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
grep -o "string" file.txt
17. Displaying the line numbers.
We can make the grep command to display the position of the line which contains the matched string in a file using the -n option
grep -n "string" file.txt
18. Displaying the position of the matched string in the line
The -b option allows the grep command to display the character position of the matched string in a file.
grep -o -b "string" file.txt
19. Matching the lines that start with a string
The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
grep "^start" file.txt
20. Matching the lines that end with a string
The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
grep "end$" file.txt
There are lots of information about latest technology and how to get trained in them, like UNIX Training have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(UNIX Training Institutes in Chennai). By the way you are running a great blog. Thanks for sharing this.
ReplyDeleteUNIX Training | UNIX Training Institutes in Chennai
Thank you for sharing valuable information
ReplyDeleteMobile app development company in chennai
Web design company in chennai
Web development company in chennai
She noticed a wide variety of pieces, with the inclusion of what it is like to have an awesome helping style to have the rest without hassle grasp some grueling matters.
ReplyDeletebest safety training in chennai
emails with the same comment. Is there any way you can remove people from that service? Thanks.
ReplyDeleteindustrial course in chennai
Your blog stuffed with lot of latest technologies news. Thanks for refreshing my mind.
ReplyDeleteSelenium Training in Chennai
Selenium Training
iOS Training in Chennai
Digital Marketing Training in Chennai
core java training in chennai
Hadoop Training Chennai
Hadoop Training in Chennai
best big data training in chennai
This is an excellant blog. Thanks for taking time to share this information. Waiting for more updates.
ReplyDeleteIELTS Tambaram
IELTS Coaching in Chennai Tambaram
IELTS Classes near me
IELTS Velachery
IELTS Training in Chennai Velachery
IELTS Training in Velachery
IELTS Coaching Centre in Velachery
I have gone through your blog, it was very much useful for me and because of your blog, and also I gained many unknown information, the way you have clearly explained is really fantastic. Kindly post more like this, Thank You.
ReplyDeletebest aviation academy in chennai
cabin crew course in chennai
diploma in airport management course in chennai
airport ground staff training in chennai
medical coding classes in Chennai
Fashion designing courses in Chennai
best interior design courses in chennai
Effective blog thanks for sharing Instagram and Facebook have provided an amazing place for new brands to grow and flourish. We can find the perfect niche for your brand on the best social media platforms.
ReplyDeletegreat very informative....
ReplyDeleteInterview Question for CTS Placement
Cognizant Interview Questions For Fresher
Cognizant Interview Questions
Resume Coustomer Service Executive
Resume For Bank Job
Resume Cyber security Engineer
Resume Data Base Developer
Resume DeputyManager
Resume Design Engineer
Resume Desktop Support Engineer
"Great post!! This can be one particular of the most useful blogs. Basically Wonderful. I can understand your hard work.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
Have you heard about the offer that is going on in our institution. So here is the offer to tell you guys. We are conducting CS executive classes and free of cost CSEET classes only for you guys . Hurry up contact us or visit our website at https://uniqueacademyforcommerce.com/
ReplyDeletenices information thanku so much this information
ReplyDeletethanku so much
nices
Trade Stocks, Forex, And Bitcoin Anywhere In The World:exness login Is The Leading Provider Of Software That Allows You To Trade On Your Own Terms. Whether You Are Operating In The Forex, Stock, cgin Software And Anonymous Digital Wallet To Connect With The Financial World.: exness login Is A Currency Trading Company That Allows You To Trade Stocks, Forex, And Cryptocurrency.
ReplyDeleteRead More About The Latest XM REVIEW Review In This Article. Learn How The Broker Operates And If You Should Avoid Trading With It Or Not.
ReplyDeleteInnovative blog thanks for sharing this information.
ReplyDeletejewellery erp software
Jewellery erp software