DEV Community

loading...
Cover image for Grep cheatsheet

Grep cheatsheet

vishnuchilamakuru profile image Vishnu Chilamakuru Originally published at vishnu.hashnode.dev ・2 min read

The grep utility searches any given input files, selecting lines that match one or more patterns. By default, a pattern matches an input line if the regular expression (RE) in the pattern matches the input line without its trailing new line. An empty expression matches every line. Each input line that matches at least one of the patterns are written to the standard output.

grep is used for simple patterns and basic regular expressions (BREs). Below are the frequent use cases which will be handy for developers in their day to day activity.

Index


Case insensitive Search

grep -i : Output of both commands below will be same as -i option does the case insensitive search.

grep -i hello file.txt
Enter fullscreen mode Exit fullscreen mode
grep -i HELLO file.txt
Enter fullscreen mode Exit fullscreen mode

Get Context

To get the context of your search use the below commands.

grep -A :

grep -A 3 hello file.txt
Enter fullscreen mode Exit fullscreen mode

This will show 3 lines of context after your match i.e "hello" word in this case.

grep -B :

grep -B 3 hello file.txt
Enter fullscreen mode Exit fullscreen mode

This will show 3 lines of context before your match i.e "hello" word in this case.

grep -C :

grep -C 3 hello file.txt
Enter fullscreen mode Exit fullscreen mode

This will show 3 lines of context before and after your match i.e "hello" word in this case.

Invert Match

grep -v: Find all lines that don't match the given pattern.

grep -v hello file.txt
Enter fullscreen mode Exit fullscreen mode

Show Filenames

grep -l: Show only the filenames of the files that matched

grep -l hello *.txt
Enter fullscreen mode Exit fullscreen mode

Above command lists all .txt files which has "hello" word in the current directory.

Recursive Search

grep -r: Search all the files in a directory

grep -r hello */*.txt
Enter fullscreen mode Exit fullscreen mode

Above command lists all .txt files inside nested directory which has "hello" word in it.

Print Matching Part

grep -o hello file.txt
Enter fullscreen mode Exit fullscreen mode

Above command prints only matching part of the line (not the complete line).

Regular Expressions

grep -E: This option is used for matching Regular Expressions.

grep -E 'hel*' file.txt
Enter fullscreen mode Exit fullscreen mode

Above command searches for all the lines which have words starting with hel.

grep -E 'hel*|wo*' file.txt
Enter fullscreen mode Exit fullscreen mode

Above command searches for all the lines which have words starting with hel OR wo

Search Binaries

grep -a: Treat binary data like its text instead of ignoring it.

Treat all files as ASCII text. Normally grep will simply print Binary file ... matches if files contain binary characters. Use of this option forces grep to output lines matching the specified pattern.

grep -a <pattern> <binary-file>
Enter fullscreen mode Exit fullscreen mode

Thank you for reading

If you like what you read and want to see more about system design, microservices and other technology-related stuff... You can follow me on Twitter here.

Buy Me A Coffee

Discussion (0)

Forem Open with the Forem app