'grep' Commands | List of Basic To Advance Use


Grep is a useful command in Linux system which is used to search a text patterns. It is most powerful & widely uses in Unix like operating systems.Below I am providing a list of some basic to advance use of grep commands.

Syntax: grep 'pattern' file name

Grep Commands

1) To search  a pattern with only first character/word of every line in a file

grep '^ first-charachter/word '  filename

example : grep '^jan' /home/user/montss.txt

2) To search a pattern with only the last charachter/ word of every line in a file

grep 'character/word$' filename

example : grep 'dec$' /home/user/months.txt

search last character or word in a file

3) To ignore the case of letter use '-i' switch to do so.

grep -i 'Aug' /sample.txt

It will ignore the upper case of Aug in file sample.txt

4) To  search in a file in except that word(remove) of the command just use '-v'

grep-v 'Aug' /sample.txt

It will search all line without containing 'Aug' in file sample.txt

5) To  count the outlput lines in your grep command use '-c'

grep '-c' 'Aug' /sample.txt

It will show you the counts of lines which have word 'Aug'

6) To list the file names which have the matching pattern

grep -l 'Aug' sample.txt example.txt

It will tell you in which file the 'Aug' pattern contain. if you want to invert the output just use L

7) To list the output of matching  pattern with number series

grep -n 'Aug' /sample.txt

It will give output with assigned  number series

8) To hide the file name from your output of matching pattern

grep -h 'Aug' /sample.txt

9) To serch a compelete word in a file

grep  -w 'keyword' filepath

10)search recursively in a folder or sub folders

 grep -r 'keyword' filepath

11)  search include line after the keyword

grep  -i -A1 'keyword' filepath

e.g  grep -i -A1 'ntp' /etc/passwd

It will search line include after the word 'ntp' 

12)  search include line before the keywords

grep -i -B2 'keyword' filepath

12) search include after and before (combine both) the keyword

grep -i -C3 'keyword ' filepath

13) search for special character (which system know as regular expression) for example literally a dot (.)

grep -i '\.' filepath

use back slash for your search because if we dont use backslash it will assume as regular expression keyword

 

Advance Uses of 'grep'

grep -E '^[A-Za-z]{3}.*@[A-Za-z].*\.(com|org|net|tv)'

This is an example of searching all valid email ids which must should start with 3 alphabets(low/upper case) then after (@) all characters should be alphabets then domain can be com,org,net,tv.  

> grep = global regular expression print

> -E = Expression

>'' =  pattern with in our search

^ = carat character for our search for first character

[A-Za-z] = search only for upper or lower case word a to z

{3} = how many number of times [letter] will search

. = any character

* = zero or many times for characters

@ = regular letter which we will search in our pattern

\. = \ use to print the meta charchter dot (.)

() = with in the brackets a word which we are searching in our patter , using pipe mentions many words


Sharing is Caring😀


grep , advance , commands, list of advance grep commands, example of grep commands , learning grep commands examples.

Post a Comment

0 Comments