Beginner's Guide to `grep`

3 years ago by root to c/linux

cross-posted from: https://lemmy.run/post/10868

Beginner's Guide to grep

grep is 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 using grep and provide you with some useful examples to get started.

Installation

grep is 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 grep is as follows:

grep [options] pattern [file(s)]
  • options: Optional flags that modify the behavior of grep.
  • pattern: The pattern or regular expression to search for.
  • file(s): Optional file(s) to search within. If not provided, grep will 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.txt

Replace "pattern" with the text you want to search for and file.txt with 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.txt

You can specify as many files as you want, separating them with spaces.

Ignoring Case

By default, grep is case-sensitive. To perform a case-insensitive search, use the -i option:

grep -i "pattern" file.txt

Displaying Line Numbers

To display line numbers along with the matching lines, use the -n option:

grep -n "pattern" file.txt

This 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 -r option (recursive search):

grep -r "pattern" directory/

Replace directory/ with the path to the directory you want to search in.

Using Regular Expressions

grep supports 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.txt

In this case, the -E option enables extended regular expressions.

Conclusion

grep is 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 using grep to 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 with grep. Happy grepping!

istdaslol 9 points 3 years ago path: 0 388176, hotness: undefined, score: 9, children: 1
root 4 points 3 years ago

I did not.

Thank you for sharing it. Something you learn everyday, eh 😀.

path: 0 388176 388235, hotness: undefined, score: 4, children: 0
null 6 points 3 years ago

This is cool, but I'd love to see some examples outputs so I can get a sense of what it actually does.

path: 0 385265, hotness: undefined, score: 6, children: 2
root 4 points 3 years ago

Sure, will try to include output in future. Appreciate the feedback.

path: 0 385265 385357, hotness: undefined, score: 4, children: 0
learnbyexample 3 points 3 years ago path: 0 385265 407246, hotness: undefined, score: 3, children: 0
james 5 points 3 years ago

Very nice guide, thanks for taking the time!

path: 0 380770, hotness: undefined, score: 5, children: 1
root 3 points 3 years ago

Thank you.

path: 0 380770 383300, hotness: undefined, score: 3, children: 0
Penguincoder 4 points 3 years ago

Good information and barely scratches the surface of grep usage. It can get a lot more complicated but also do a lot more than you think.

Two of my most used grep invocations are:

  • Diff two files, showing lines: grep -xvFf (file2) (file1)
  • Show lines that do not contain a string: grep -rivE '^#' (file) ( only shows uncommented lines for most Linux configuration files)
path: 0 398043, hotness: undefined, score: 4, children: 0
0x4E4F 4 points 3 years ago

You can live without vim, but not without cat and grep.

path: 0 382413, hotness: undefined, score: 4, children: 2
js10 1 point 3 years ago

Very true and I always combine them when I dont need to, using cat file.txt | grep foo instead of just grep foo file.txt

path: 0 382413 413805, hotness: undefined, score: 1, children: 1
0x4E4F 2 points 3 years ago

Yeah, IDK why, just feels kinda natural.

path: 0 382413 413805 413809, hotness: undefined, score: 2, children: 0
curioushom 4 points 3 years ago

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 grep and 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 ifconfig into grep:

ifconfig | grep wire

path: 0 381387, hotness: undefined, score: 4, children: 1
root 3 points 3 years ago

Thank you

path: 0 381387 384620, hotness: undefined, score: 3, children: 0
RomanRoy 3 points 3 years ago

Others have already mentioned man grep or grep help

But, in case you don't know about it, there are two great utilities to get examples and help for almost any given command: tldr and cheat are great.

https://github.com/cheat/cheat

https://github.com/tldr-pages/tldr

Just cheat grep or tldr grep and you're good to go :)

path: 0 401752, hotness: undefined, score: 3, children: 0
donio 3 points 3 years ago

A few more options that I use pretty much daily:

  • -v for reversing the match (display non-matching instead of matching)
  • -e to specify multiple patterns, matching any one is sufficient
  • -w to match only at word boundaries, easier to type than the equivalent regexp
  • -c for displaying the match count rather than the matches

And some that that I use occasionally:

  • -NUM, -B NUM, -A NUM also show NUM lines around/before/after match
  • -l to display only the filenames with the matches
  • -F "fixed" pattern meaning literal match only rather than regexp. Great to avoid having to quote regexp special characters when you don't need regexp matching
  • -P for PCRE style regexps
  • -f to read match patterns from a file
  • -q quiet, only produce exit status, no output. Useful in shell expressions (scripts, one liners).
  • -a force treating the input as text (useful to override the binary detection heuristic. mnemonic: ascii)
path: 0 412524, hotness: undefined, score: 3, children: 0
TunaCowboy 2 points 3 years ago

Beginner’s Guide to grep

man grep

path: 0 396978, hotness: undefined, score: 2, children: 0
TacoDog 2 points 3 years ago

Would you mind explaining this regular expression a little, and how it's grabbing email addresses? What is "\b"? What does "+" do? And the {2,}?

path: 0 409997, hotness: undefined, score: 2, children: 3
rattenjunge 2 points 3 years ago

+ means that of the prepending selection of characters (those in the []-brackets) at least one character has to Match.

The curly braces are some advanced Form of that and means for {n,m} anything between n and m characters of the prepending selection. In this Case it would be at least 2 as m isn't given. This will Match any Domain ending With at least 2 characters.

Notice however that this is a very Basic regex pattern to Match Email addresses.

path: 0 409997 411332, hotness: undefined, score: 2, children: 1
TacoDog 1 point 3 years ago

I always though you needed to put a star (*) after the brackets to match multiple of the same pattern. The pattern ".*" is multiple of anything right? Been using Linux for 20 years and I still need to look this up every time. There seems to be different regex patterns for bash (where just "*" is multiple of anything) than for sed, awk and grep. I've still never seen the \b syntax.

path: 0 409997 411332 416495, hotness: undefined, score: 1, children: 0
Noodlez 1 point 3 years ago

I highly recommend reading man 7 regex or if you're a more visual and play with learner, use RegExr. It's even better if you use the two together.

path: 0 409997 416071, hotness: undefined, score: 1, children: 0
LemoineFairclough 1 point 3 years ago path: 0 3797253, hotness: undefined, score: 1, children: 0
js10 1 point 3 years ago

Fantastic summary of one of the most universally used cli tool. One thing to add is that the name grep comes from the ed command g/re/p which stands for "global, regular expression, print" since thats what it does; search everything (global) for a given regular expression (even if the "regular expression" is just a specific string to match) and prints it out. Keeping the name origin in mind helps me to remember what it does.

path: 0 421336, hotness: undefined, score: 1, children: 0
progenyofthestars 1 point 3 years ago

Thank you, always fun to read these and learn about an option you haven't used yourself yet.

path: 0 381026, hotness: undefined, score: 1, children: 0
mark 1 point 3 years ago

If you are serious about working in a terminal, then I highly recommend learning modern replacements for the old tools.

In this case ripgrep (or rg) https://github.com/BurntSushi/ripgrep is phenomenal. Especially for searching recursively in a large directory tree it is unbelievably quicker than regular grep.

It won't be installed on any random machine, so grep is still useful, but if you regularly need to text search in files then there are better tools.

path: 0 381351, hotness: undefined, score: 1, children: 1
root 3 points 3 years ago

Yeap, but most of the time you end up trying to figure out issue on remote system, where you don't have ripgrep always installed, but if you have that available on the system you are working on. ripgrep is always a better alternative.

path: 0 381351 381523, hotness: undefined, score: 3, children: 0
Kernel 1 point 3 years ago

Though related, it's often conflated with regular expressions as a concept. On that note, services like RegExr let you test patterns and their effects visually.

path: 0 381453, hotness: undefined, score: 1, children: 1
PrecisePangolin 1 point 3 years ago

Another good one is rubular.com

path: 0 381453 400631, hotness: undefined, score: 1, children: 0
StefanT 1 point 3 years ago

grep -v inverts the search. All lines that do not match are printed.

path: 0 411769, hotness: undefined, score: 1, children: 0
bizdelnick 1 point 3 years ago

"Using regular expressions" is misleading. A beginner could think that by default grep is looking for a literal string, but it does not.

path: 0 1249875, hotness: undefined, score: 1, children: 0
Clav64 1 point 3 years ago

Bookmarked for later, thank you kind lemming.

path: 0 419140, hotness: undefined, score: 1, children: 0
linux
linux

@lemmy.ml

login for more options
68145
9469
5670

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

  • Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
  • No misinformation
  • No NSFW content
  • No hate speech, bigotry, etc

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

go to feed...