Questions tagged [grep]

grep is a command-line text-search utility originally written for Unix. It uses regular expressions to match text, and is commonly used as a filter in pipelines. Use this tag only if your question relates to programming using grep or grep-based APIs. Questions relating to using or troubleshooting grep command-line options itself are off-topic.

Grep

The name comes from and similar editors, and is derived from global / regular expression / print.

Grep is a command-line utility for searching plain-text data sets for lines matching a regular expression. Grep was originally developed for the Unix operating system, but is available today for all Unix-like systems.

There are many programming languages which include a command or statement called grep; please simply use that language's tag for questions which do not pertain to the Unix utility.

Variations

The following are the several implementations of grep available in some Unix environments:

  • : same as grep -E - Interprets the pattern as an extended regular expression.
  • : same as grep -F - Interprets the pattern as a list of fixed strings, separated by newlines, any of which is to be matched.
  • : displays the processes whose names match a given regular expression.

Common options

  • -f file - Obtain patterns from file, one per line.
  • -i - Ignore case distinctions in both the pattern and the input files.
  • -o - Show only the part of a matching line that matches the pattern.
  • -c - Print a count of matching lines for each input file.
  • -R - Read all files under each directory recursively.
  • -v - Invert the sense of matching to select non-matching lines.

Frequently asked

Other Stack Exchange sites

References

  1. grep/egrep/fgrep man page
  2. pgrep man page
  3. POSIX grep man page
  4. GNU grep manual

Books

  • grep Pocket Reference - "A quick pocket reference for a utility every Unix user needs"
  • GNU GREP and RIPGREP - Step by step guide with hundreds of examples and exercises. Includes detailed discussion on BRE/ERE/PCRE(2)/Rust regular expressions used in these commands.
16456 questions
340
votes
6 answers

Display filename before matching line

How can I get grep to display the filename before the matching lines in its output?
Vivek
  • 4,452
  • 10
  • 27
  • 45
339
votes
2 answers

How to suppress binary file matching results in grep

When using grep in linux, the result often contains a lot of "binary file XXX matches", which I do not care about. How to suppress this part of the results, or how to exclude binary files in grep?
RandyTek
  • 4,374
  • 3
  • 23
  • 32
336
votes
10 answers

Highlight text similar to grep, but don't filter out text

When using grep, it will highlight any text in a line with a match to your regular expression. What if I want this behaviour, but have grep print out all lines as well? I came up empty after a quick look through the grep man page.
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
333
votes
16 answers

Count all occurrences of a string in lots of files with grep

I have a bunch of log files. I need to find out how many times a string occurs in all files. grep -c string * returns ... file1:1 file2:0 file3:0 ... Using a pipe I was able to get only files that have one or more occurrences: grep -c string * |…
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
331
votes
18 answers

How can I find all of the distinct file extensions in a folder hierarchy?

On a Linux machine I would like to traverse a folder hierarchy and get a list of all of the distinct file extensions within it. What would be the best way to achieve this from a shell?
GloryFish
  • 13,078
  • 16
  • 53
  • 43
311
votes
5 answers

How to invert a grep expression

The following grep expression successfully lists all the .exe and .html files in the current directory and sub directories. ls -R |grep -E .*[\.exe]$\|.*[\.html]$ How do I invert this result to list those that aren't a .html or .exe instead.…
sMaN
  • 3,667
  • 4
  • 23
  • 33
301
votes
17 answers

How to get the process ID to kill a nohup process?

I'm running a nohup process on the server. When I try to kill it my putty console closes instead. this is how I try to find the process ID: ps -ef |grep nohup this is the command to kill kill -9 1787 787
user2535056
  • 4,003
  • 5
  • 17
  • 13
297
votes
4 answers

Grep regex NOT containing a string

I am passing a list of regex patterns to grep to check against a syslog file. They are usually matching an IP address and log entry; grep "1\.2\.3\.4.*Has exploded" syslog.log It's just a list of patterns like the "1\.2\.3\.4.*Has exploded" part I…
jwbensley
  • 10,534
  • 19
  • 75
  • 93
292
votes
8 answers

What is makeinfo, and how do I get it?

I'm trying to build GNU grep, and when I run make, I get: [snip] /bin/bash: line 9: makeinfo: command not found What is makeinfo, and how do I get it? (This is Ubuntu, if it makes a difference)
mike
  • 46,876
  • 44
  • 102
  • 112
288
votes
28 answers

What are good grep tools for Windows?

Any recommendations on grep tools for Windows? Ideally ones that could leverage 64-bit OS. I'm aware of Cygwin, of course, and have also found PowerGREP, but I'm wondering if there are any hidden gems out there?
Portman
  • 31,785
  • 25
  • 82
  • 101
288
votes
12 answers

How can I have grep not print out 'No such file or directory' errors?

I'm grepping through a large pile of code managed by git, and whenever I do a grep, I see piles and piles of messages of the form: > grep pattern * -R -n whatever/.git/svn: No such file or directory Is there any way I can make those lines go away?…
alexgolec
  • 26,898
  • 33
  • 107
  • 159
287
votes
15 answers

How to search contents of multiple pdf files?

How could I search the contents of PDF files in a directory/subdirectory? I am looking for some command line tools. It seems that grep can't search PDF files.
Jestin Joy
  • 3,711
  • 4
  • 19
  • 14
284
votes
24 answers

Match two strings in one line with grep

I am trying to use grep to match lines that contain two different strings. I have tried the following but this matches lines that contain either string1 or string2 which not what I want. grep 'string1\|string2' filename So how do I match with grep…
hearsaxas
  • 2,851
  • 2
  • 15
  • 4
284
votes
3 answers

Regex (grep) for multi-line search needed

I'm running a grep to find any *.sql file that has the word select followed by the word customerName followed by the word from. This select statement can span many lines and can contain tabs and newlines. I've tried a few variations on the…
Ciaran Archer
  • 12,316
  • 9
  • 38
  • 55
283
votes
3 answers

grep without showing path/file:line

How do you grep and only return the matching line? i.e. The path/filename is omitted from the results. In this case I want to look in all .bar files in the current directory, searching for the term FOO find . -name '*.bar' -exec grep -Hn FOO {} \;
Allan Thomas
  • 3,481
  • 5
  • 26
  • 29