Search For The Given String in A Single File
Search For The Given String in A Single File
This is also a basic usage of grep command. For this example, let us copy the demo_file to
demo_file1. The grep output will also include the file name in front of the line that matched the
specific pattern as shown below. When the Linux shell sees the meta character, it does the
expansion and gives all the files as input to grep.
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
This is also a basic usage of the grep. This searches for the given string/pattern case insensitively.
So it matches all the words such as the, THE and The case insensitively as shown below.
$ grep -i "the" demo_file
This is a very powerful feature, if you can use use regular expression effectively. In the following
example, it searches for all the pattern that starts with lines and ends with empty with
anything in-between. i.e To search lines[anything in-between]empty in the demo_file.
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
From documentation of grep: A regular expression may be followed by one of several repetition
operators:
{n,m} The preceding item is matched at least n times, but not more than m times.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
The following example is the WORD grep where it is searching only for the word is. Please
note that this output does not contain the line This Line Has All Its First Character Of The Word
With Upper Case, even though is is there in the This, as the following is looking only for
the word is and not for this.
$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
e
E
b
B
w
W
go
go
go
go
go
go
to
to
to
to
to
to
the
the
the
the
the
the
The following example prints the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
When you had option to show the N lines after match, you have the -B option for the opposite.
$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
10. display the lines which does not matches all the given pattern.
Syntax:
grep -v -e "pattern" -e "pattern"
$ cat test-file.txt
a
b
c
d
$ grep -v -e "a" -e "b" -e "c" test-file.txt
d
When you want to count that how many lines matches the given pattern/string, then use the
option -c.
Syntax:
grep -c "pattern" filename
$ grep -c "go" demo_text
6
When you want do find out how many lines matches the pattern
$ grep -c this demo_file
3
When you want do find out how many lines that does not match the pattern
$ grep -v -c this demo_file
4
12. Display only the file names which matches the given pattern using grep -l
If you want the grep to show out only the file names which matched the given pattern, use the -l
(lower-case L) option.
When you give multiple files to the grep as input, it displays the names of file which contains the
text that matches the pattern, will be very handy when you try to find some notes in your whole
directory structure.
$ grep -l this demo_*
demo_file
demo_file1
When you want grep to show the position where it matches the pattern in the file, use the
following options as
Syntax:
grep -o -b "pattern" file
$ cat temp-file.txt
12345
12345
$ grep -o -b "3" temp-file.txt
2:3
8:3
Note: The output of the grep command above is not the position in the line, it is byte offset of the
whole file.
15. Show line number while displaying the output using grep -n
To show the line number of file with the line matched. It does 1-based line numbering for each
file. Use -n option to utilize this feature.
$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.