DronaBlog

Wednesday, September 12, 2018

How to use 'grep' commands in Unix?

Are you looking for  details about 'grep' commands in the Unix environment? Are you also looking for what are structures and samples for various 'grep' commands in the Unix system? If so, then this article provides detailed information about 'grep' command with its usage. 

What is the 'grep' command?

The word grep stands for globally search a regular expression and print. The command 'grep' is a command-line utility. It is used for searching plain-text data. The search is performed by using a regular expression.

Commands:

1. Use the command below to search a specific string in the specified file
grep "Techno Guru" test_file

2. Use the command below to search a specific string in all the files
grep "Techno Guru" *

3. Use the command below to search a specific string in only in the .log  files. We can also use regular expressions such as abc*.log, *test*.*, abc*.log. Search will be performed against files which matches this file patterns.
grep "Techno Guru" *.log

4. Use the command below to perform case in-sensitive search. The command below will matches all the words such as "TECHNO GURU", "Techno Guru", "tEchno Guru", "techno guru" etc.
grep -i "Techno Guru" test_file

5. To print the matched line, along with 5 lines after it.
grep  -A 5 -i "Techno Guru" test_file

6. To perform recursive search
grep -r "Techno Guru" test_file

7. To perform recursive search in all the files. Below command searches "Techno Guru" word in all the files under the current directory and its sub directory
grep -r "Techno Guru" *

8. Use the command below to search using regular expression in search string. The command below searches for starting string "Techno" and ends with "Guru". You can use other different search pattern as well.
grep "Techno*Guru" test_file

9. What are the regular patterns can be used?
The below mentioned regular patterns can be used while working with grep command.
? : Matched at most once.
* : Matched zero or more times.
+  : Matched one or more times.
{n} : Matched exactly n times.
{n,} : Matched n or more times.
{,m}  : Matched at most m times.
{n,m} : Matched at least n times, but not more than m times.

10. Use 'grep -w' command to search for full words and not for sub-strings. In the below command exact "Techno" or "techno" or "TECHNO" will be matched in the file. However if file contains "Technoworld" then it will not be identified as match.
grep -iw "Techno" test_file

11. Displaying lines before/after/around the match using grep -A, -B and -C
If you are performing file analysis in real time project, it will be useful to see some lines after or before the match.

a) To display 5 lines after match
grep -A 5 -i "Techno Guru" test_file

b) To display 5 lines before match
grep -B 5 "Techno Guru" test_file

c) To display 4 lines around match. The option -C used for the match to be appeared with the lines from both the side.
grep -C 4 "Techno Guru" test_file

12. To count the number of matches use the command below
grep -c "Technology World"  Test_file

13. To count the number lines which does NOT found match, use the command below
grep -v -c "Technology World"  Test_file

14. To highlight the search. In order to see which part matches the line, we can highlight it as (Use can use any color to highlight)
export GREP_OPTIONS='--color=auto' GREP_COLOR='99;8'

15. To determine the name of files in which match string found.
grep -l "Techno" test_*

16. Use the command below to show only matched string as by default grep command shows the line which matches the given string.
grep -o "Techno Guru" Test_file

Learn more about Unix in the video below:


No comments:

Post a Comment

Please do not enter any spam link in the comment box.

Understanding Survivorship in Informatica IDMC - Customer 360 SaaS

  In Informatica IDMC - Customer 360 SaaS, survivorship is a critical concept that determines which data from multiple sources should be ret...