24 examples for grep
# 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 *
# Filter out unwanted results. (This example will show all files in a directory that are not ruby files.) ls | grep -v *.rb
# grep recursively through files of a certain type, such as .py files grep -R --include="*.py" "pattern" /path/to/dir
# 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
# Searches for the exact string "jdk1.7.0", treating '.' as a period rather than as any character grep -F "jdk1.7.0"
# 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
# Case insensitve search through all of the files recursively starting from my current directory for the string "computesSum". grep -ir computesSum *
# Filter lines not containing a match for "my process" from a text stream tail -f "my_server.log" | grep "my process"
# 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" .
# Search for `foo` within files up to the specified directory depth find . -maxdepth 1 -exec grep foo {} \;
# show filenames of all files containing the pattern in this folder # and all sub folders grep -rl "pattern" .
# match lines that contain foo or bar in $file_in and append them to $file_out grep '^.*\(foo\|bar\).*$' $file_in > $file_out
# Search all files in the current diectory recursively for a string matching the regex pattern and return only the file, line number, and the matched string. grep -Porn 'regex pattern' ./*
# Obtener el numero de veces que aparece una palabra segun el resultado del filtro grep -c bash /etc/passwd
# Filtra todas las lineas que no contengan "nologin" en /etc/passwd grep -v nologin /etc/passwd
# Filtra las lineas de un archivo en /etc que contiene cualquier caso, del patron de caracter "linux" grep -i linux /etc/* # Filtra las lineas del archivo en /etc que contenga el patron de caracter "linux" grep -w linux /etc/
# Print all lines from 'file' that don't contain any numbers grep -v "[0123456789]" file