Questions tagged [grep]

For questions pertaining to grep, a command-line tool for searching text patterns in files. Use this tag for questions about grep itself or questions about issues arising from using the grep command-line tool.

The grep utility searches a pattern (regular expression) in a text file. There are many options to control how matches are performed and how to display matches.

The name grep comes from the ed (a Unix line editor) command g/re/p which means “globally search for a regular expression and print all lines containing it”.

External references

Tutorials

Further reading

4769 questions
782
votes
13 answers

How do I grep for multiple patterns with pattern having a pipe character?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and complains when bar isn't an executable. How can I…
Dan
  • 9,122
  • 5
  • 24
  • 38
508
votes
8 answers

Can grep output only specified groupings that match?

Say I have a file: # file: 'test.txt' foobar bash 1 bash foobar happy foobar I only want to know what words appear after "foobar", so I can use this regex: "foobar \(\w\+\)" The parenthesis indicate that I have a special interest in the word right…
Cory Klein
  • 17,221
  • 26
  • 78
  • 92
392
votes
17 answers

How can I prevent 'grep' from showing up in ps results?

When I search for some process that doesn't exist, e.g. $ ps aux | grep fnord wayne 15745 0.0 0.0 13580 928 pts/6 S+ 03:58 0:00 grep fnord Obviously I don't care about grep - that makes as much sense as…
Wayne Werner
  • 11,151
  • 8
  • 27
  • 41
361
votes
7 answers

Count total number of occurrences using grep

grep -c is useful for finding how many times a string occurs in a file, but it only counts each occurence once per line. How to count multiple occurences per line? I'm looking for something more elegant than: perl -e '$_ = <>; print scalar ( () =…
user4518
304
votes
5 answers

What is the difference between `grep`, `egrep`, and `fgrep`?

Can anyone tell me the technical differences between grep, egrep, and fgrep and provide suitable examples? When do I need to use grep over egrep and vice versa?
Vishwanath Dalvi
  • 4,256
  • 5
  • 18
  • 17
238
votes
18 answers

How can I grep in PDF files?

Is there a way to search PDF files using grep, without converting to text first in Ubuntu?
Dervin Thunk
  • 3,149
  • 4
  • 22
  • 21
232
votes
10 answers

What makes grep consider a file to be binary?

I have some database dumps from a Windows system on my box. They are text files. I'm using cygwin to grep through them. These appear to be plain text files; I open them with text editors such as notepad and wordpad and they look legible. However,…
user394
  • 13,574
  • 20
  • 64
  • 90
227
votes
6 answers

Can grep return true/false or are there alternative methods

As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message; if it doesn't, append the arguments to the file. I understand how to write the if statement, but…
Lauren
  • 2,335
  • 2
  • 15
  • 9
183
votes
6 answers

Return only the portion of a line after a matching pattern

So pulling open a file with cat and then using grep to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a pattern, but only to return the portion of the line…
MaQleod
  • 2,424
  • 4
  • 21
  • 18
179
votes
6 answers

How do I grep recursively through .gz files?

I am using a script to regularly download my gmail messages that compresses the raw .eml into .gz files. The script creates a folder for each day, and then compresses every message into its own file. I would like a way to search through this archive…
Kendor
  • 1,891
  • 2
  • 11
  • 5
178
votes
5 answers

grep returns "Binary file (standard input) matches" when trying to find a string pattern in file

I'm on Ubuntu and I typed cat .bash_history | grep git and it returned Binary file (standard input) matches My bash_history does exist and there are many lines in it that starts with git. What caused it to display this error and how can I fix it?
answerSeeker
  • 2,107
  • 3
  • 13
  • 15
159
votes
6 answers

How can I count the number of lines of a file with common tools?

I am redirecting grep results to a file, and then using cat to show its contents on the screen. I want to know how many lines of results I have in my results file and then add it to some counter. What will be the best way? Any relevant flag to grep…
k-man
152
votes
13 answers

Convince grep to output all lines, not just those with matches

Say I have the following file: $ cat test test line 1 test line 2 line without the search word another line without it test line 3 with two test words test line 4 By default, grep returns each line that contains the search term: $ grep test…
Michael Mrozek
  • 87,955
  • 34
  • 234
  • 230
143
votes
9 answers

How to run grep with multiple AND patterns?

I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence: grep pattern1 | grep pattern2 | ... So how to convert it to something like? grep pattern1 & pattern2 &…
greenoldman
  • 5,806
  • 16
  • 52
  • 65
136
votes
11 answers

How to grep standard error stream (stderr)?

I am using ffmpeg to get the meta info of an audio clip. But I am unable to grep it. $ ffmpeg -i 01-Daemon.mp3 |grep -i Duration FFmpeg version SVN-r15261, Copyright (c) 2000-2008 Fabrice Bellard, et al. configuration: --prefix=/usr…
Andrew-Dufresne
  • 5,373
  • 7
  • 24
  • 19
1
2 3
99 100