Grep command tips
Posted by Md. Mahidul Hasan on 6:24 AM with No comments
Grep command tips:
### Search /path/to/file for tom user:
root@host:~# grep tom /etc/passwd
### Ignore word case sencevity:
root@host:~# grep -i "boo" /etc/passwd
### grep recursively (read all files under each directory for a string "192.168.1.5")
root@host:~# grep -r "192.168.1.5" /etc/
### grep to search words only
root@host:~# grep -w "boo" /path/to/file
### grep to search 2 different words
root@host:~# egrep -w 'word1|word2' /path/to/file
### Count line when words has been matched
root@host:~# grep -c 'word' /path/to/file
### grep with line number
root@host:~# grep -n 'word' /path/to/file
### Grep invert match
root@host:~# grep -v bar /path/to/file
### grep command often used with pipes. For example print name of hard disk devices:
root@host:~# dmesg | egrep '(s|h)d[a-z]'
### Use the -l option to list file name whose contents mention main():
root@host:~# grep -l 'main' *.c
### grep to display output in colors:
root@host:~# grep --color vivek /etc/passwd
### Highlighting the search using GREP_OPTIONS
root@host:~# export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
### grep with additional output lines
root@host:~# grep -A 3 -i "example" demo_text
-A is the option which prints the specified N lines after the match as shown below.
root@host:~# grep -B 2 "single WORD" demo_text
-B is the option which prints the specified N lines before the match.
root@host:~# grep -C 2 "Example" demo_text
-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.
### Search /path/to/file for tom user:
root@host:~# grep tom /etc/passwd
### Ignore word case sencevity:
root@host:~# grep -i "boo" /etc/passwd
### grep recursively (read all files under each directory for a string "192.168.1.5")
root@host:~# grep -r "192.168.1.5" /etc/
### grep to search words only
root@host:~# grep -w "boo" /path/to/file
### grep to search 2 different words
root@host:~# egrep -w 'word1|word2' /path/to/file
### Count line when words has been matched
root@host:~# grep -c 'word' /path/to/file
### grep with line number
root@host:~# grep -n 'word' /path/to/file
### Grep invert match
root@host:~# grep -v bar /path/to/file
### grep command often used with pipes. For example print name of hard disk devices:
root@host:~# dmesg | egrep '(s|h)d[a-z]'
### Use the -l option to list file name whose contents mention main():
root@host:~# grep -l 'main' *.c
### grep to display output in colors:
root@host:~# grep --color vivek /etc/passwd
### Highlighting the search using GREP_OPTIONS
root@host:~# export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
### grep with additional output lines
root@host:~# grep -A 3 -i "example" demo_text
-A is the option which prints the specified N lines after the match as shown below.
root@host:~# grep -B 2 "single WORD" demo_text
-B is the option which prints the specified N lines before the match.
root@host:~# grep -C 2 "Example" demo_text
-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.
Categories: Grep command tips
0 comments:
Post a Comment