19 examples for grep

{{ score }}
  # Case insensitive search through all of the files recusively starting from my current directory for the string "computesSum" but does not search binary files.  When a match is found, print the line number it was found.
grep -iIrn computesSum *
        
{{ score }}
  # search for a string pattern in a file
grep "string pattern" "filename"
        
{{ score }}
  # Filter out unwanted results. (This example will show all files in a directory that are not ruby files.)
ls | grep -v *.rb
        
{{ score }}
  # grep recursively through files of a certain type, such as .py files 
grep -R --include="*.py" "pattern" /path/to/dir
        
{{ score }}
  # Searches for the exact string "jdk1.7.0", treating '.' as a period rather than as any character
grep -F "jdk1.7.0"
        
{{ score }}
  # grep for IP addresses
grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' input.txt > output.txt
        
{{ score }}
  # Case insensitve search through all of the files recursively starting from my current directory for the string "computesSum".  
grep -ir computesSum *
        
{{ score }}
  # grep for a word and get the lines before and after the occurance
# in this case 3 lines before and after
grep -C 3 "word" ./file.txt
        
{{ score }}
  # Show file name in output
grep -H search_string file_name
        
{{ score }}
  # Filter lines not containing a match for "my process" from a text stream
tail -f "my_server.log" | grep "my process"
        
{{ score }}
  # recursively search all files of a specified type for a search string (case insensitive)
# E.g. seach all python files in a directory (and its sub-directories) for the expression "fixme"
grep -ri --include="*.py" -e "fixme" .
        
{{ score }}
  # Find files without pattern
grep -L pattern *
        
{{ score }}
  # Search for `foo` within files up to the specified directory depth
find . -maxdepth 1 -exec grep foo {} \;
        
{{ score }}
  # prints lines beginning with "#", ^ refers to beginning of line
grep "^#"
        
{{ score }}
  # Show only the first match in each file 
grep -m 1 "needle" *.txt
        
{{ score }}
  # show filenames of all files containing the pattern in this folder
# and all sub folders
grep -rl "pattern" .
        
{{ score }}
  # Force line buffering
mysqladmin processlist | grep --line-buffered "Query"
        
{{ score }}
  # match lines that contain foo or bar in $file_in and append them to $file_out 
grep '^.*\(foo\|bar\).*$' $file_in > $file_out
        
{{ score }}
  # Print all lines from 'file' that don't contain any numbers
grep -v "[0123456789]" file