cross-posted from: https://lemmy.run/post/10868
Beginner’s Guide to
grep
grepis a powerful command-line tool used for searching and filtering text in files. It allows you to find specific patterns or strings within files, making it an invaluable tool for developers, sysadmins, and anyone working with text data. In this guide, we will cover the basics of usinggrepand provide you with some useful examples to get started.Installation
grepis a standard utility on most Unix-like systems, including Linux and macOS. If you’re using a Windows operating system, you can install it by using the Windows Subsystem for Linux (WSL) or through tools like Git Bash, Cygwin, or MinGW.Basic Usage
The basic syntax of
grepis as follows:grep [options] pattern [file(s)]
options: Optional flags that modify the behavior ofgrep.pattern: The pattern or regular expression to search for.file(s): Optional file(s) to search within. If not provided,grepwill read from standard input.Examples
Searching in a Single File
To search for a specific pattern in a single file, use the following command:
grep "pattern" file.txtReplace
"pattern"with the text you want to search for andfile.txtwith the name of the file you want to search in.Searching in Multiple Files
If you want to search for a pattern across multiple files, use the following command:
grep "pattern" file1.txt file2.txt file3.txtYou can specify as many files as you want, separating them with spaces.
Ignoring Case
By default,
grepis case-sensitive. To perform a case-insensitive search, use the-ioption:grep -i "pattern" file.txtDisplaying Line Numbers
To display line numbers along with the matching lines, use the
-noption:grep -n "pattern" file.txtThis can be helpful when you want to know the line numbers where matches occur.
Searching Recursively
To search for a pattern in all files within a directory and its subdirectories, use the
-roption (recursive search):grep -r "pattern" directory/Replace
directory/with the path to the directory you want to search in.Using Regular Expressions
grepsupports regular expressions for more advanced pattern matching. Here’s an example using a regular expression to search for email addresses:grep -E "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" file.txtIn this case, the
-Eoption enables extended regular expressions.Conclusion
grepis a versatile tool that can greatly enhance your text searching and filtering capabilities. With the knowledge you’ve gained in this beginner’s guide, you can start usinggrepto quickly find and extract the information you need from text files. Experiment with different options and explore more advanced regular expressions to further expand your skills withgrep. Happy grepping!
Others have already mentioned
man greporgrep helpBut, in case you don’t know about it, there are two great utilities to get examples and help for almost any given command:
tldrandcheatare great.https://github.com/cheat/cheat
https://github.com/tldr-pages/tldr
Just
cheat greportldr grepand you’re good to go :)Did you know the whole grep program was written within a day, by non other then Ken Thompson https://youtube.com/watch?v=NTfOnGZUZDk&feature=share7
“Using regular expressions” is misleading. A beginner could think that by default
grepis looking for a literal string, but it does not.Excellent guide, thanks for the write up!
One thing I’d like to point out is that you can pipe output from an application into
grepand then be able to use all the information above.For instance if I want to know the full name of my wireguard interface I can just pass (pipe) the output of
ifconfigintogrep:ifconfig | grep wireBeginner’s Guide to
grepman grepYou can live without vim, but not without cat and grep.
Very true and I always combine them when I dont need to, using
cat file.txt | grep fooinstead of justgrep foo file.txtYeah, IDK why, just feels kinda natural.
If you are serious about working in a terminal, then I highly recommend learning modern replacements for the old tools.
In this case
ripgrep(orrg) https://github.com/BurntSushi/ripgrep is phenomenal. Especially for searching recursively in a large directory tree it is unbelievably quicker than regulargrep.It won’t be installed on any random machine, so
grepis still useful, but if you regularly need to text search in files then there are better tools.Very nice guide, thanks for taking the time!
Thank you.




