GREP command in Linux
The GREP is abbreviation of "Global Regulation Expression Pattern". GREP command is a pattern search tool provided by Linux.
By default it searches all matching lines from the file or files for the searched word. There are lot of switches(options) available with GREP command to search information from files.
Syntax for GREP command :
-----
grep word filename
grep word file1 file2 file3 file4
grep "string1 string2" filename
cat filename | grep word
command | grep word
-----
Some examples of GREP command :
1). grep command to search a file
------
# grep hoststud /home/user
------
2). grep to ignore word case :
------
# grep -i "sys" /etc/passwd
------
3). grep command to use recursively read all files under sub-directories :
------
grep -r "1x8.xx.58.28x" /var/accesslogs
------
4). grep command to search exact word in file :
-------
# grep -w 'darren' /var/domlogs
-------
5). grep command to search two different words :
-------
# grep 'word1'|'word2' /path/of/the/file
-------
6). grep command to count number of lines in which words matched :
-------
# grep -c 'word' /path/to/the/file
-------
7). grep command to precede each line of output with the number of the line in the text file :
-------
# grep -n 'word' /path/of/the/file
-------
8). grep command to give invert match(match those that do not contain the input word) :
-------
# grep -v 'word' /path/of/the/file
-------
9). grep commnad to list just the names of matching files :
-------
# grep -l 'word' *
-------
10). grep command to force display the output in colors :
-------
# grep --color 'word' path/of/the/file
-------
The GREP is abbreviation of "Global Regulation Expression Pattern". GREP command is a pattern search tool provided by Linux.
By default it searches all matching lines from the file or files for the searched word. There are lot of switches(options) available with GREP command to search information from files.
Syntax for GREP command :
-----
grep word filename
grep word file1 file2 file3 file4
grep "string1 string2" filename
cat filename | grep word
command | grep word
-----
Some examples of GREP command :
1). grep command to search a file
------
# grep hoststud /home/user
------
2). grep to ignore word case :
------
# grep -i "sys" /etc/passwd
------
3). grep command to use recursively read all files under sub-directories :
------
grep -r "1x8.xx.58.28x" /var/accesslogs
------
4). grep command to search exact word in file :
-------
# grep -w 'darren' /var/domlogs
-------
5). grep command to search two different words :
-------
# grep 'word1'|'word2' /path/of/the/file
-------
6). grep command to count number of lines in which words matched :
-------
# grep -c 'word' /path/to/the/file
-------
7). grep command to precede each line of output with the number of the line in the text file :
-------
# grep -n 'word' /path/of/the/file
-------
8). grep command to give invert match(match those that do not contain the input word) :
-------
# grep -v 'word' /path/of/the/file
-------
9). grep commnad to list just the names of matching files :
-------
# grep -l 'word' *
-------
10). grep command to force display the output in colors :
-------
# grep --color 'word' path/of/the/file
-------