grep command tutorial
What is grep?
grep stands for Globally search a Regular Expression and Print. Created by Ken Thompson, the author of B Programming Language. It is a command-line utility to search a particular string in the files.
Usage 1: Search a particular word in file
To search a particular word in a file.
$ grep <word> <file to search>
Eg. grep hello words.txt
The above command searches for word "hello" in the file "words.txt" and prints the matching lines in which the word "hello" is present in the file .
The above command by default performs case sensitive search, this means if there is "Hello" present in the words.txt file it won't be detected by the search.
Usage 2: Case Insensitive word search in file
$grep -i <word> <file to search>
Eg: grep -i hello words.txt
The above command performs case insensitive search and gives you "hello" "Hello" or other variants in the search.
Usage 3: Count the number of times a word repeats
$grep -c <word> <file to search>
Eg: grep -c hello words.txt
Will print the number of times the word hello is present in the words.txt file. This command is similar to $grep hello words.txt | wc -l
You can also combine the insensitive search also in this.
Eg: grep -i -c hello words.txt
Will gives number of times the word hello is present and the search is case insensitive.
Usage 4: Print Line numbers in the search
$grep -n <word> <file to search>
Eg: grep -n hello words.txt
will print the line numbers along with the matching lines present in the words.txt file
grep stands for Globally search a Regular Expression and Print. Created by Ken Thompson, the author of B Programming Language. It is a command-line utility to search a particular string in the files.
Usage 1: Search a particular word in file
To search a particular word in a file.
$ grep <word> <file to search>
Eg. grep hello words.txt
The above command searches for word "hello" in the file "words.txt" and prints the matching lines in which the word "hello" is present in the file .
The above command by default performs case sensitive search, this means if there is "Hello" present in the words.txt file it won't be detected by the search.
Usage 2: Case Insensitive word search in file
$grep -i <word> <file to search>
Eg: grep -i hello words.txt
The above command performs case insensitive search and gives you "hello" "Hello" or other variants in the search.
Usage 3: Count the number of times a word repeats
$grep -c <word> <file to search>
Eg: grep -c hello words.txt
Will print the number of times the word hello is present in the words.txt file. This command is similar to $grep hello words.txt | wc -l
You can also combine the insensitive search also in this.
Eg: grep -i -c hello words.txt
Will gives number of times the word hello is present and the search is case insensitive.
Usage 4: Print Line numbers in the search
$grep -n <word> <file to search>
Eg: grep -n hello words.txt
will print the line numbers along with the matching lines present in the words.txt file
Comments
Post a Comment