bash grep rgrep - ghdrako/doc_snipets GitHub Wiki

rgrep - recursive grep

W wielu systemach rgrep to alias na grep -R

grep alternatives

ack,ag,ripgrep

Grep

grep <pattern> a.txt
grep <pattern> a.txt b.txt
grep <pattern> *.txt

grep -r <pattern> ./* # scan files in current dir and subdirs 
grep -iw "jest" plik # wyszukanie bez rozroznienia wielkosci liter (-i) 
                     # dokladnego dopasowania slowa(-w)jest. 
                     # Aby zostalo dopasowane musi miec spacje lub znak interpunkcyjny po jednej ze stron
# two command do the same
find dir -name "*.js" | xargs grep "describe"  # better for debug
grep -r --include "*.js" "describe" dir
git grep "describe"  # grep ignore all files git ignore and match result highlighted in red
grep -r --color "describe" # match result highlighted in red

logical AND and logical OR operations

grep abc *sh | grep def # matches lines containing abc AND
def
grep abc\|def *sh  # matches lines containing abc OR def

Options

  • The -c option counts the number of occurrences of a string: even though ABC4.sh has no matches, it still counts them and returns zero:
grep –c abc *sh
The output of the preceding command is here:
ABC4.sh:0
abc3.sh:3
``
* The ``-e`` option lets you match patterns that would otherwise cause syntax problems (the – character normally is interpreted as an argument or grep):
``
grep –e -abc *sh
abc3.sh:ends with -abc

The -e option also lets you match multiple patterns.

grep –e -abc -e comment *sh
ABC4.sh:# ABC in a comment
abc3.sh:ends with -abc
  • The -a option search binaries: treat binaries as textinstead of ignoring it
  • The -i option performs a case insensitive match
  • The -v option “inverts” the matching string
  • The -l option lists only the filenames that contain a successful match
  • The -n option specifies the line numbers of any matching file
  • The -h option suppresses the display of the filename for a
  • The -o option shows only the matched string (this is how you avoid returning the entire line that matches):
  • The -o option followed by the –b option shows the position of the matched string (it returns the character position, not the line mber. The "o" in "one" is the 59th character of the file):
grep –o –b one columns4.txt
  • The -r option specify a recursive search This searches not only every file in directory /etc, but every file in every subdirectory of etc):
grep –r abc /etc

Add context to results

  • -B n before
  • -A n after
  • -C n context
grep -n "#" *.md  # add line number in result

grep -A 2 --color "#" *.md # show matched line and two after

grep -B 2 --color "#" *.md # show matched line and two before

grep -C 2 --color "#" *.md # show matched line and two before and after C - context

grep -r -n -C 2 --color "#" *.md 

Pattern - sepecial character

grep --color "http." *.md # dot match exacly one character

grep --color "https\?" *.md # s match zero or one time \? - match http and https

grep --color "###\+" *.md # s match one or more times \+ - match  https

grep --color "\.com" *.md # escape - not interprete as special
grep --color "##*" *.md # glob wildcard character
grep --color "(.*)" *.md # glob wildcard character

Extended syntax -E - not need escape special character - using regexp like egrep

grep --color -E "https?" *.md # s match zero or one time ? - match http and https
grep --color -E "https+" *.md # s match one or more times + - match  https
grep --color  "https+" *.md # withow escaping or flag -E is treat as ordinary char

Alternative

echo "is grey or gray ?"|grep --color "grey\|gray"
echo "is grey or gray ?"|grep --color -E "grey|gray"
grep --color -rE "grey|gray" /examples

Position in the line

grep --color "^#" *.md # at the begining of the line match hash(#) 
grep --color ",$" *.md # at the end of the line match comma(,) 
grep --color -r "^import .* from" /examples

Class character

echo abc123| grep --color "[ab]"
echo abc123| grep --color "[a-z]"
echo abc123| grep --color "[1-3]"
grep --color  "de[a-z]*er" *.md # match developer designer ...
grep --color  "de[[:alpha]]*er" *.md # match developer designer ...
grep --color "[sS]pec" *.md 

Grouping using ()

grep --color -rE "(grey|gray)\'"  .  # escape single quote to not interprete
grep --color -rE "(grey|gray)(\'|\")#?[[:digital]]+" .

Exclude with flag -v

find examples/angular -name "*.js" | grep -v "node_modules" 
find examples/angular -name "*.js" | grep -vE "node_modules|Spec" 

Recursive - search in folders

# recurisve grep on current folder
grep -R "createEmployee" .
# recursieve grep on .java files
grep -R "createemployee" *.java

# recursive grep on src/ directory
grep -R "createemployee" src/

# recursive grep on directories starts with 'sr'
grep -R "createEmployee" sr*

Search for a "pattern" inside all files in the current directory

grep -rn "pattern"
grep -RnisI "pattern" *
fgrep "pattern" * -R

Show only for multiple patterns

grep 'INFO*'\''WARN' filename
grep 'INFO\|WARN' filename
grep -e INFO -e WARN filename
grep -E '(INFO|WARN)' filename
egrep "INFO|WARN" filename

Except multiple patterns

grep -vE '(error|critical|warning)' filename

Show data from file without comments

grep -v ^[[:space:]]*# filename

Show data from file without comments and new lines

egrep -v '#|^$' filename

Show strings with a dash/hyphen

grep -e -- filename
grep -- -- filename
grep "\-\-" filename

Remove blank lines from a file and save output to new file

grep . filename > newfilename

Exit codes

  • 0: Match found.
  • 1: No match found.
  • 2: Error (bad regex, file issues, etc.)
grep "search_pattern" file.txt
if [ $? -eq 0 ]; then
echo "Pattern found!"
elif [ $? -eq 1 ]; then
echo "Pattern not found."
else
echo "An error occurred during the search."
fi
⚠️ **GitHub.com Fallback** ⚠️