Difference between revisions of "Linux comand: grep"

From RHS Wiki
Jump to navigation Jump to search
Line 1: Line 1:
exclude words in grep search:
+
Command-line utility to search lines matching regular expresions in plain-text data sets.
grep -v "text to exclude"
+
 
 +
{| class="wikitable"
 +
|-
 +
! Option !! Description !! Example
 +
|-
 +
| -c || Count || grep -c 'root' /etc/passwd
 +
|-
 +
| -e || Specify multiple search patterns || grep -e 'root' -e 'system' /etc/passwd
 +
|-
 +
| -r || Recursive || Example
 +
|-
 +
| -v || Show lines not matching the pattern || Example
 +
|-
 +
| -i || Case insensitive || Example
 +
|-
 +
| -n || Output line numbering || Example
 +
|-
 +
| -E || Enable regex use (similar to egrep) || Example
 +
|-
 +
| -o || Show only the match, not the full line || Example
 +
|-
 +
| -f FILE || Example || Example
 +
|-
 +
| -H || Print file name || Example
 +
|-
 +
| Example || Example || Example
 +
|}
 +
 
 +
Examples:
 +
<source lang="bash">
 +
grep '\<a.*\>' archivo
 +
cat archivo | grep "\<a.*\>"
 +
grep "#" /boot/grub/menu.lst
 +
grep -v "#" /boot/grub/menu.lst
 +
grep -c "iface" /etc/network/interfaces
 +
grep -e "root" -e "password" archivo
 +
grep -n -e "root" -e "password" archivo
 +
grep -r "password" *
 +
ifconfig eth0 | grep -oiE '([0-9A-F]{2}:){5}[0-9A-F]{2}' # Show eth0 MAC address
 +
grep -Eio '[a-z0-9._-]+@[a-z0-9.-]+[a-z]{2,4}' file.txt  # Extract e-main addresses from file.txt
 +
 
 +
 
 +
</source>

Revision as of 08:18, 11 November 2015

Command-line utility to search lines matching regular expresions in plain-text data sets.

Option Description Example
-c Count grep -c 'root' /etc/passwd
-e Specify multiple search patterns grep -e 'root' -e 'system' /etc/passwd
-r Recursive Example
-v Show lines not matching the pattern Example
-i Case insensitive Example
-n Output line numbering Example
-E Enable regex use (similar to egrep) Example
-o Show only the match, not the full line Example
-f FILE Example Example
-H Print file name Example
Example Example Example

Examples:

grep '\<a.*\>' archivo
cat archivo | grep "\<a.*\>"
grep "#" /boot/grub/menu.lst
grep -v "#" /boot/grub/menu.lst
grep -c "iface" /etc/network/interfaces
grep -e "root" -e "password" archivo
grep -n -e "root" -e "password" archivo
grep -r "password" *
ifconfig eth0 | grep -oiE '([0-9A-F]{2}:){5}[0-9A-F]{2}' # Show eth0 MAC address
grep -Eio '[a-z0-9._-]+@[a-z0-9.-]+[a-z]{2,4}' file.txt  # Extract e-main addresses from file.txt