We can grep the text as required, searching with particular string, printing lines before and after the search string.
First I am going to create an file to explain how grep works. The demo files contains below lines
1. Check grep command installed..? and its version
Redhat / Centos / Fedora use below ccommand
In Ubuntu Operating system
To check its version in all the OS
2. Search single word in single file / multiple files case sensitive
To grep text as case sensitive no need to use any options, default grep will search the text as case sensitive. See the below example when searched with ‘first‘ word it does find matching in the file but when we search with ‘First‘ it find matching.
3. Search case insensitive word using grep -i
To grep text as case insensitive we have to use -i option. So it matches all the words such as “first”, “FIRST” and “First” case insensitively as shown below.
4. Search text which is not matching to string
Below option is useful when your searching for the exclude matching word. As a example below it is excluded “First” line from the search.
You can also use multiple strings using -e option. see the other below example where we are excluding the ‘First‘ and ‘last‘ strings.
5. Print the matching string and its after number of lines
To print the string and its after number of lines, we have to use -A option. See the below example grepping the word ‘below‘ and mentioned number of lines after -A options it prints after immediate lines.
6. Print the matching string and its before number of lines
To print the string and its before number of lines, we have to use -B option. See the below example grepping the word ‘below’ and mentioned number of lines after -B option it prints before number of lines.
7. Print the matching string and its around number of lines
To print matching string and its around lines we have to use -C option. See the below example grepping for the word ‘ALL’ and mentioned number of lines after -C option it print its above and its below line.
8. Search Recursively all the sub-directories
To search all the sub-directories we have to use -r flag. See the below example to understand the recursive search.
9. Grep the string with highlighted in color
Most of the times we search for matching strings but we have see in detailed that where is matched string is. If we see searched string in will show in highlighted color it will be most effective view we can see. So how we can set the grep highlight color lets see
We can use –color option to see the string in color, as a temporary.
If you want to set this option as permanent we have to set the environment variable
which basically highlights the matched pattern with foreground color black and background color yellow (shown below in the snap).
The set display attributes list:
0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden
Foreground Colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
Background Colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
10. Get the count of given string from single file / multiple files
To count the matched string we have to use -c option. See the below example.
11. Search for files which are matching to the given string
We can also search the files using grep command lets see how to search files. Below example we are searching for the demo* file.
12. Beginning of line (^) using cap symbol
In grep command, caret Symbol ^ matches the expression at the start of a line. In the following example, it displays all the line which starts with the Oct 05. i.e All the messages logged on October 05.
The ^ matches the expression in the beginning of a line, only if it is the first character in a regular expression. ^N matches line beginning with N.
13. End of the line ( $) using dollar symbol
Character $ matches the expression at the end of a line. The following command will help you to get all the lines which ends with the word ‘interrupt’
From the above output you can come to know when all the messages has got interrupt. Just like ^ matches the beginning of the line only if it is the first character, $ matches the end of the line only if it is the last character in a regular expression.
This grep commands are most useful commands when we want to search for some strings in the files, searching for the particular files on the directories.
Do comment you feedback about this article.
No comments:
Post a Comment