We are using grep command for searching content in a file. Here i’m explaining the most useful grep commands using in linux.linux security,

Example text file – I’m using this text file to explain all the below examples.

# cat lin1.txt
This is my test file.
I’m using vim editor here

- Linuxinternetworks.com

1. Find out a word or phrase in and under the specified directory

# grep -inr “file” ./*
./lin1.txt:1:This is my test file.

This example will find the word “file” in and under the current directories. It will search all the files and directories in the present working directory to find out the matched pattern.

2. Find out a word or phrase in a file (case-sensitive)

# grep “file” lin1.txt
This is my test file.

This example will find the word “file” in the file lin1.txt and it is case-sensitive.

3. Find out a word or phrase in a file (case-insensitive)

# grep -i “linuxinternetworks” lin1.txt
- Linuxinternetworks.com

This example will find out the word “linuxinternetworks” in the file lin1.txt and it is case-insensitive.

4. Use regular expression to find out a word or phrase. Here use “.*” for missing letters or words

# grep -i “Lin.*net.*s.com” lin1.txt
- Linuxinternetworks.com

This example contains regular expression and we can use “.*” to match certain characters.

5. Matching more patterns. Option “-e” used to match more than one pattern in grep command. It allows you to find out more than one word or phrase in the specified text file.

# grep -e “file” -e “editor” lin1.txt
This is my test file.
I’m using vim editor here

6. Invert matching. Sometimes you want to find out invert patters in a file or directory. In that case you should use option “-v” which allows you find out inverted pattern matching in grep. This performs NOT operation

# grep -v “file” lin1.txt
I’m using vim editor here

- Linuxinternetworks.com

7. Counting patterns – To count the number of available matched pattern in a file or multiple files you should the option “-c”

# grep -c “e” lin1.txt
3
# grep -c “editor” lin1.txt
1

8. Find out filenames which matches the patterns. Sometimes we have patterns and wants to find out the filenames which matches those patterns. For that in grep command you should use the option “-l”

# grep -l “This” ./*
./lin1.txt
./lin2.txt
./lin3.txt

9. Matches “n” lines after pattern matching. Where n is the number of lines to match after the search pattern. You should use the option “-A” in the grep command

# grep -A 1 “This” lin1.txt
This is my test file.
I’m using vim editor here

In this example, it displays 1 line after the search pattern “This”

10. Matches “n” lines before pattern matching. Where n is the number of lines to match before the search pattern. You should use the option “-B” in the grep command

# grep -B 1 “editor” lin1.txt
This is my test file.
I’m using vim editor here

In this example, it displays 1 line before the search pattern “editor”

11. Matches “n” lines around pattern matching. Where n is the number of lines to match around the search pattern. You should use the option “-C” in the grep command

# grep -C 2 “editor” lin1.txt
This is my test file.
I’m using vim editor here

- Linuxinternetworks.com

In this example, it displays 2 line before the search pattern “editor”

12. Display only matched string – The option “-o” allows you get only the matched pattern.

# grep -o “editor” lin1.txt
editor

13. Find out matched pattern with the line number. You should use the option “-n”

# grep -n “editor” lin1.txt
2:I’m using vim editor here

14. Grep command with AND operator

There is no AND operator for grep command. But you can use it using the below method

# grep -E “I’m.*here” lin1.txt
I’m using vim editor here

15. Grep Command with OR operator

You can use OR operator in two methods. See the below examples

# grep ‘using\|hello’ lin1.txt
I’m using vim editor here

# grep ‘using\|here’ lin1.txt
I’m using vim editor here

# grep -E ‘using|hello’ lin1.txt
I’m using vim editor here

# grep -E ‘using|here’ lin1.txt
I’m using vim editor here

16. Grep command with NOT operator. It works like Invert matching pattern as explained above. You should use option “-v”

# grep -v “using” lin1.txt
This is my test file.

- Linuxinternetworks.com

Related articles


Subscribe Our Free Newsletter

Delivered by FeedBurner. We Respect Your Privacy

Tagged with:
 

Leave a Reply