Meta advice: "tldr" (tldr.sh/) is a wonderful, pragmatic way to find common flags for commands. It's much more approachable than man.
$ tldr grep
grep
Matches patterns in input text.
Supports simple patterns and regular expressions.
- Search for an exact string:
grep search_string path/to/file
- Search in case-insensitive mode:
grep -i search_string path/to/file
- Search recursively (ignoring non-text files) in current directory for an exact string:
grep -RI search_string .
- Use extended regular expressions (supporting `?`, `+`, `{}`, `()` and `|`):
grep -E ^regex$ path/to/file
- Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match:
grep -C|B|A 3 search_string path/to/file
- Print file name with the corresponding line number for each match:
grep -Hn search_string path/to/file
- Use the standard input instead of a file:
cat path/to/file | grep search_string
- Invert match for excluding specific strings:
grep -v search_string
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Suggestion: as a good practice, always use single quotes unless you need double quotes (you've mixed it up in the article)
With GNU grep, you have two options to control how to separate different matching groups: --no-group-separator and --group-separator (see my tutorial for details)
As pointed by others, ripgrep has --passthru option to show entire input file instead of using regex trick. You can also use -E '^|pattern' or -E 'pattern|$' instead of -e option. always color option is useful to highlight multiple terms in different color, see stackoverflow.com/questions/172360... for example
For those interested in detailed learning of GNU grep and ripgrep, I have a book on that: github.com/learnbyexample/learn_gn... (I had a free offer running, but that ended yesterday, bad timing)
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Very helpful, thanks! I appreciate the input from someone who literally wrote the book.
The —always tip is especially good, because I was not sure that there was ever a good reason for that option. The -e choice of syntax was deliberate, as I think it gives an ever so slight readability boost over the more opaque regex syntax for showing separate patterns, but showing folks that the extended regex flag is available is a good tip. Thanks again!
2nd Meta advice: grep is awfully slow, there are many replacement such as The Platinum Searcher or - my personal favourite - RipGrep. Both support almost the same options as grep, so switching from grep to one of those program will be easy.
Data wrangler, software engineer, systems programmer, cyclist. Unix (mostly Solaris) for aeons. I talk C, Python, SQL, Performance, Java, Kafka and Makefiles.
Location
Brisbane, Australia
Education
BA (Mathematics, Modern History), University of Queensland
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Data wrangler, software engineer, systems programmer, cyclist. Unix (mostly Solaris) for aeons. I talk C, Python, SQL, Performance, Java, Kafka and Makefiles.
Location
Brisbane, Australia
Education
BA (Mathematics, Modern History), University of Queensland
No, because that's not its job. If you want search highlighting use a colourised grep. If you just want to see the whole file, use cat. Do one thing, do it well.
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Yeah cool, just checking. Thanks for clarifying. This post was mostly about different ways to see more of the file as you searched for specific things, but you are right that if all you want to do is see the contents of the file, cat is the best option for short files. I appreciate the addition!
Meta advice: "tldr" (tldr.sh/) is a wonderful, pragmatic way to find common flags for commands. It's much more approachable than
man.Wow... a lot nicer than
manIt's best to also use them in conjunction:
Use tldr for the quick easy things, then with that knowledge, you know what to look for in the man page.
Nice! That’s a neat service. Thanks for sharing!
If I remember correctly, you can install tldr locally on your PC.
You absolutely can: tldr.sh/#installation
Nicely presented!
Suggestion: as a good practice, always use single quotes unless you need double quotes (you've mixed it up in the article)
With
GNU grep, you have two options to control how to separate different matching groups:--no-group-separatorand--group-separator(see my tutorial for details)As pointed by others,
ripgrephas--passthruoption to show entire input file instead of using regex trick. You can also use-E '^|pattern'or-E 'pattern|$'instead of-eoption.alwayscolor option is useful to highlight multiple terms in different color, see stackoverflow.com/questions/172360... for exampleFor those interested in detailed learning of
GNU grepandripgrep, I have a book on that: github.com/learnbyexample/learn_gn... (I had a free offer running, but that ended yesterday, bad timing)Very helpful, thanks! I appreciate the input from someone who literally wrote the book.
The —always tip is especially good, because I was not sure that there was ever a good reason for that option. The -e choice of syntax was deliberate, as I think it gives an ever so slight readability boost over the more opaque regex syntax for showing separate patterns, but showing folks that the extended regex flag is available is a good tip. Thanks again!
2nd Meta advice: grep is awfully slow, there are many replacement such as The Platinum Searcher or - my personal favourite - RipGrep. Both support almost the same options as grep, so switching from grep to one of those program will be easy.
An even better way of "seeing the whole file" is to use
cat... though if you only want a page or screenful at a time thenlessis definitely more.Does cat do search highlighting?
No, because that's not its job. If you want search highlighting use a colourised grep. If you just want to see the whole file, use cat. Do one thing, do it well.
Yeah cool, just checking. Thanks for clarifying. This post was mostly about different ways to see more of the file as you searched for specific things, but you are right that if all you want to do is see the contents of the file, cat is the best option for short files. I appreciate the addition!
No, but you can alias cat to bat
As an aside,
ripgrepandackcan do what you want with the--passthruflag:You can omit the -A -B -C and just go for
grep -n4 pattern file
To get indexed result with 4 lines above and below
What about ack? Once I discovered ack-grep I never went back.