Some history of grep command before moving to its practical use:
- Grep is a command line utility used in Unix and Linux to find/search text.
- Grep stands for "g/re/p" ↔ globally search a regular expression and print stands
- Grep was originally developed only for UNIX OS. However, later it was available for other OS which are similar to UNIX.
- Grep was created overnight by Ken Thompson to help him analyze text while fixing some bug which had large amount of text.
- Grep was first included in Unix version 4.
- Early variants of grep included 'egrep' and 'fgrep'
Lets dive in to its basic uses:
- Syntax : To search single string in single file. ↠ grep "string" filename
- Syntax : To search single string in multiple files. ↠ grep "string" filename*
- Syntax : To search case insensitive string ↠ grep -i "string" filename
Other Uses of Grep Command :
- To use grep recursively ↠ grep -r "string" filename
- To suppress output (filename) ↠ grep -hR "string" filename
- To search invert using grep ↠ grep -v "string" filename
- To use grep with shell pipe ↠ dmesg | egrep "string"
- To use grep with cat command ↠ cat filename | grep 'string'
- To count amount of string in file using grep ↠ grep -c "string" filename
grep command options
|
Description
|
-i
|
Ignore case sensitivity
|
-w
|
To match exact string
|
-v
|
Select invert lines from file
|
-n
|
Print line number with output
|
-h
|
Suppress the file name prefix in output
|
-r
|
Search directories recursively
|
-l
|
Print only names of file with selected lines
|
-c
|
Print only a count of selected lines per FILE
|
No comments:
Post a Comment