| Line 3: |
Line 3: |
| | {| class="wikitable" | | {| class="wikitable" |
| | |- | | |- |
| − | ! Option !! Description !! Example | + | !Option!!Description!!Example |
| | |- | | |- |
| − | | -c || Count || grep -c 'root' /etc/passwd | + | | -c||Count||grep -c 'root' /etc/passwd |
| | |- | | |- |
| − | | -e || Specify multiple search patterns || grep -e 'root' -e 'system' /etc/passwd | + | | -e||Specify multiple search patterns||grep -e 'root' -e 'system' /etc/passwd |
| | |- | | |- |
| − | | -r || Recursive || | + | | -r||Recursive|| |
| | |- | | |- |
| − | | -v || Show lines not matching the pattern || | + | | -v||Show lines not matching the pattern|| |
| | |- | | |- |
| − | | -i || Case insensitive || | + | | -i||Case insensitive|| |
| | |- | | |- |
| − | | -n || Output line numbering || | + | | -n||Output line numbering|| |
| | |- | | |- |
| − | | -E || Enable regex use (similar to egrep) || | + | | -E||Enable regex use (similar to egrep)|| |
| | |- | | |- |
| − | | -o || Show only the match, not the full line || | + | | -o||Show only the match, not the full line|| |
| | |- | | |- |
| − | | -f FILE || Specify file to search in. || | + | | -f FILE||Specify file to search in.|| |
| | |- | | |- |
| − | | -H || Print file name || | + | | -H||Print file name|| |
| | + | |- |
| | + | | -P |
| | + | |Perl Regex |
| | + | |grep -P '\d{16}' |
| | |} | | |} |
| | | | |
| Line 40: |
Line 44: |
| | grep -Eio '[a-z0-9._-]+@[a-z0-9.-]+[a-z]{2,4}' file.txt # Extract e-main addresses from file.txt</source> | | grep -Eio '[a-z0-9._-]+@[a-z0-9.-]+[a-z]{2,4}' file.txt # Extract e-main addresses from file.txt</source> |
| | | | |
| − | == Find files containing text pattern == | + | ==Find files containing text pattern== |
| | grep -rnw '/path/' -e "pattern" | | grep -rnw '/path/' -e "pattern" |
| | | | |
| − | * -r or -R is recursive, | + | *-r or -R is recursive, |
| − | * -n is line number, and | + | *-n is line number, and |
| − | * -w stands match the whole word. | + | *-w stands match the whole word. |
| − | * -l (lower-case L) can be added to just give the file name of matching files. | + | *-l (lower-case L) can be added to just give the file name of matching files. |
| | + | |
| | --exclude or --include parameter could be used for efficient searching. Something like below: | | --exclude or --include parameter could be used for efficient searching. Something like below: |
| | grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern" | | grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern" |
| | grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern" | | grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern" |
| | grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern" | | grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern" |
| | + | |
| | + | ==Remove comments and blank lines== |
| | + | cat /etc/apache2/sites-enabled/default-ssl.conf | grep -v '^$\|^\s*\#' |
| | + | |
| | + | ==Grep with blanks== |
| | + | <syntaxhighlight lang="bash"> |
| | + | cat dig_output.txt | grep -E 'INblank:A' |
| | + | cat LogsAndreaPasswords.csv | awk -F ',' '{print $1 "," $2}' | uniq -c | sort -n | grep '^\s*1 ' |
| | + | </syntaxhighlight> |
| | + | |
| | + | ==Grep remove comments and blank lines== |
| | + | <syntaxhighlight lang="bash"> |
| | + | cat file.txt | grep -v ^\#|grep . |
| | + | </syntaxhighlight> |