Home
Search for the given string in a single file
Back To Top
Home
Search for the given string in a single file
# grep "this" demo_file
Checking for the given string in multiple files
# grep "this" demo_*
Case insensitive search using grep -i
# grep -i "the" demo_file
Match regular expression in files
# grep "lines.*empty"
demo_file
Checking for full words, not for sub-strings
using grep –w If you want to search for a word, and to avoid it to match the
substrings use -w option. Just doing out a normal search will show out all the
lines.
Following example is the regular grep where it
is searching for 'is'. When you search for 'is', without any option it will
show out 'is', 'his', 'this' and everything which has the substring 'is'.
# grep -i "is" demo_file
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
Displaying lines before/after/around
the match using grep -A, -B and -C
The following example prints the matched line,
along with the 3 lines after it.
# grep -A 3 -i "example"
demo_text
The following example prints the
matched line, along with the 2 lines before it.
# grep -B 2 "single WORD"
demo_text
The following example prints the
matched line, along with the 2 lines before and after it.
# grep -C 2 "Example"
demo_text
Highlighting the search using
GREP_OPTIONS
As grep prints out lines from the
file by the pattern / string you had given, if you wanted it to highlight which
part matches the line, then you need to follow the following way
# export
GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
# grep this demo_file
Searching in all files recursively
using grep -r
# grep -r "ramesh" *
Invert match using grep -v
When you want to display the lines
which does not matches the given string/pattern, use the option -v as shown
below.
# grep -v "go" demo_text
# grep -i
"go" demo_text
Display the lines which does not
matches all the given pattern.
# grep -v -e "pattern"
demo_text
Counting the number of matches using grep -c
When you want to count that how many lines
matches the given pattern/string, then use the option -c as shown below,
# grep -c "go" demo_text
When you want do find out how many
lines matches the pattern
# grep -c this demo_file
When you want do find out how many lines that
does not match the pattern
# grep -v -c this demo_file
Display only the file names which
matches the given pattern using grep -l
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_*
Show only the matched string
By default grep will show the line
which matches the given pattern/string, but if you want the grep to show out
only the matched string of the pattern then use the -o option.
It might not be that much useful when you give
the string straight forward. But it becomes very useful when you give a regex
pattern and trying to see what it matches as
# grep -o "is.*line"
demo_file
Show the position of match in the
line
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.
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
Home
No comments:
Post a Comment