Grep cheatsheet

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…


This content originally appeared on DEV Community and was authored by Vishnu Chilamakuru

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
grep -i HELLO file.txt

Get Context

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

grep -A :

grep -A 3 hello file.txt

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

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

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

Show Filenames

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

grep -l hello *.txt

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

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

Print Matching Part

grep -o hello file.txt

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

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

grep -E 'hel*|wo*' file.txt

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>

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


This content originally appeared on DEV Community and was authored by Vishnu Chilamakuru


Print Share Comment Cite Upload Translate Updates
APA

Vishnu Chilamakuru | Sciencx (2021-04-27T08:21:55+00:00) Grep cheatsheet. Retrieved from https://www.scien.cx/2021/04/27/grep-cheatsheet/

MLA
" » Grep cheatsheet." Vishnu Chilamakuru | Sciencx - Tuesday April 27, 2021, https://www.scien.cx/2021/04/27/grep-cheatsheet/
HARVARD
Vishnu Chilamakuru | Sciencx Tuesday April 27, 2021 » Grep cheatsheet., viewed ,<https://www.scien.cx/2021/04/27/grep-cheatsheet/>
VANCOUVER
Vishnu Chilamakuru | Sciencx - » Grep cheatsheet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/27/grep-cheatsheet/
CHICAGO
" » Grep cheatsheet." Vishnu Chilamakuru | Sciencx - Accessed . https://www.scien.cx/2021/04/27/grep-cheatsheet/
IEEE
" » Grep cheatsheet." Vishnu Chilamakuru | Sciencx [Online]. Available: https://www.scien.cx/2021/04/27/grep-cheatsheet/. [Accessed: ]
rf:citation
» Grep cheatsheet | Vishnu Chilamakuru | Sciencx | https://www.scien.cx/2021/04/27/grep-cheatsheet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.