grep Command in Linux/Unix
grep command in Unix is used for pattern or expression searching. It has many OPTIONS that allows to perform various file search operations.
In this article, we will learn common grep command usage with examples:
- what is grep command in Linux/ Unix?
- -n (–line-number) OPTION with grep
- -c (–count) OPTION with grep
- -v (–invert-match) OPTION with grep
- -i (–ignore-case) OPTION with grep
- -l (–files-with-matches) OPTION with grep
- -w (–word-regexp) OPTION with grep
- -o (–only-matching) OPTION with grep
- -r (-recursive) OPTION with grep
grep command in Linux/ Unix
- Grep full form is “Globally Search For Regular Expression and Print out”.
- grep is a pattern or expression searching command.
- Prints found matches.
- Grep comes with a number of OPTIONS that allows to perform various search operations on files.
SYNTAX: $grep[options] "pattern to be matched" [filename ]
-v Shows all the lines that do not match the searched string
-c Displays only the count of matching lines
-n Shows the matching line and its number
-i Match both (upper and lower) case
-l Shows just the name of the file with the string
$grep 'hello' file1.txt Output : searches hello in the file1.txt and outputs/returns the lines containing 'hello'.
Grep command can also be used with meta-characters:
Input : $grep 'test' * Output : it searches for test in all the files and directories. * is a meta-character and returns matching 0 or more preceding characters
-n (–line-number) OPTION with grep command
- grep -n option prints out the matches for the text along with the line numbers.
- Lists line numbers
$ grep -n 'root' /etc/passwd Output: 1:root:x:0:0:root:/root:/bin/bash 1042:rootmask:x:0:0:rootmask:/home/rootmask:/bin/csh
The results has line numbers for the text matches.
-c (–count) OPTION with grep command in Unix
- grep -c option prints the number of lines of matches
$grep -c 'test' file1.txt Output: 2 Note: If there are more than 1 searched word 'test' on line one, option -c would still print 2. This is because it is concerned with the number of lines where the matches appear, not the number of matches.
-v (–invert-match) OPTION with grep command in Unix
- grep -v option inverts the match, it matches only those lines that do not contain the given word.
$grep -v 'test' file1.txt Result: Hi All We are learning
The output does not contains the searched pattern.
Note :You can use -n option along with -v t list the line numbers.
-i (–ignore-case) OPTION with grep command in Unix
- grep -i option is used to ignore-case sensitivity
$grep -i 'tEst' file1.txt No output $grep -i 'tEst' file1.txt Output: In test Test class
-l (–files-with-matches) OPTION with grep command
- grep -l option print file names that match a pattern
$grep -l test *.txt Output: file1.txt
-w (–word-regexp) OPTION with grep command
- grep -w option searches for the line containing the exact matching word.
- By default, grep matches strings which contain the specified pattern.
- This means that grep ‘test’ file1.txt will print the same results as grep ‘est’ file.txt because ‘est’ can be found in test.
- With the option -w, grep ensures that the matches are exactly the same pattern as specified.
$grep -w est file1.txt Output: No results
$grep -i -w test file1.txt Output: In test Test class
-o (–only-matching) OPTION with grep command
- grep -o otion prints only the matched pattern
- By default, grep prints the line where the matched pattern is found.
- With option -o, only the matched pattern is printed line by line.
$grep -i -o yo file1.txt Output: test Test
-r (-recursive) OPTION with grep command
- It is used for recursive search.
- By default, grep cannot search directories, you will get an error (“Is a directory”).
- With option -R, searching files within directories and subdirectories becomes possible.
$grep -r test * Output: myData/file1.txt: In test myData/file2.txt: Test to learning
for more example refer here