Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

Tuesday, March 12, 2019

GREP unix command useful tips!!!

Recently I learned useful tips from my manager for 'grep' which helps a lot when troubleshooting the issues with logs. In this post I will explain about below

  • How to search multiple strings in a file at the same time.
  • How to display found string in the color.
  • How to exclude particular string in the search result
  • How to display total no.of lines in the grep result


Search a string in a file using grep: Check this link for basic usage of grep command.
Lets see this tip with example. Below is the content of a file demo.txt and this file name we are going to use in this example as well.




How to search multiple strings in a file:
In the below screen we are search for the strings 'demo', 'show' and 'multiple' in the file demo.txt and and the result is as follows.


How to display found string in color:
In the above screen, even though grep found the patterns, it is difficult to identify in which line these patterns available. For that you can use grep command property 'color' as shown below.



From the above screen, we can easily identify the found strings as they are highlighted in red color. This color utility will save lot of time when you are searching in debug logs while troubleshooting.

How to exclude particular string in the search result: GREP command will supports option to exclude particular pattern from the result. This is basically not including a string in the result.



In the above screen, initially search for the strings 'demo' and 'show' and in the results I want to exclude the string 'multiple'.

How to display total no.of lines in the grep result: We can use -c option to get the total number of lines of the grep result. If the pattern is unique in each line, that count will be total no.of occurrences of the pattern.



In the above screen, searching for the patterns 'demo' and 'show' results two lines and using -c option will show the total count to two. If the grep results are more, this count option will be very useful.


Happy Learning!!!

Thursday, March 15, 2012

GREP command in UNIX!!

The grep is a very powerfull search command in Unix. It is used to search the string or pattern or regular exression in a file line by line and displays the line which contains the given pattern. grep stands for global regular expression parser or print. The grep family includes grep, egrep, fgrep.

Syntax:

grep [options] pattern filename

grep: is a orginal command, its basic use to select the line and search for the string
egrep: is Extended grep and it uses extended regular expression (supporting logical OR in pattern) to select the lines to process.
fgrep: is a Fast grep and it will not use the regular expressions, but it uses string literals to process.

Example for grep:

$grep ^[aeiou] test.txt
in a file line by
or print. The grep family
includes grep, egrep, fgrep
a son of srking.
a son of srqueen .

The above grep command searches for the patterns starting with vowel small letters in the starting of the line in the file test.txt and displays them

Example for egrep:

$egrep 'jr|sr|super ' test.txt
jrking is
a son of srking.
srking is a son of supersrking.
supersking grand sun is jrking.

$grep 'jr|sr|super ' test.txt
$


The above egrep searches for the strings jr or sr or super in test.txt file. single quotes are mandatory. grep displays nothing as it doesnot support logical OR.


Example for fgrep:

$fgrep king test.txt
jrking is
a son of srking.
srking is a son of supersrking.
supersking grand sun is jrking.

$fgrep ^[aeiou] test.txt
$

From the above examples, in the first fgrep is looking for the string literal 'king' in test.txt and displays the lines which contains the 'king' literal. Where as in second command fgrep is looking for the regular expression ^[aeiou] and it displays nothing as fgrep doesn't support regular expressions. If you use grep instead of fgrep , you will get the result as shown in example for grep above.

Some of usefull grep options:

-i : ignore the case
-r: search for the pattern recursively (usefull for searching in the directory/sub-directory)
-n: displays the lines with line no.s
-c: displays the count of the found patterns
-l: displays the file name which contains the given pattern (this is usefull while searching in sub-directories)

Subscribe to: Posts (Atom)

Popular Posts

AltStyle によって変換されたページ (->オリジナル) /