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
660
votes
24 answers

Colorized grep -- viewing the entire file with highlighted matches

I find grep's --color=always flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it…
zslayton
  • 51,416
  • 9
  • 35
  • 50
651
votes
36 answers

Delete all local git branches

I follow a development process where I create a new local branch for every new feature or story card. When finished I merge the branch into master and then push. What tends to happen over time due to a combination of laziness or forgetfulness, is…
Louth
  • 11,401
  • 5
  • 28
  • 37
648
votes
9 answers

How can I exclude one word with grep?

I need something like: grep ^"unwanted_word"XXXXXXXX
john
  • 6,515
  • 3
  • 15
  • 4
534
votes
22 answers

grep a tab in UNIX

How do I grep tab (\t) in files on the Unix platform?
Sachin
  • 20,805
  • 32
  • 86
  • 99
532
votes
7 answers

Grep only the first match and stop

I'm searching a directory recursively using grep with the following arguments hoping to only return the first match. Unfortunately, it returns more than one -- in-fact two the last time I looked. It seems like I have too many arguments, especially…
Tim Kamm
  • 5,421
  • 2
  • 13
  • 3
506
votes
10 answers

Capturing Groups From a Grep RegEx

I've got this script in sh (macOS 10.6) to look through an array of files: files="*.jpg" for f in $files do echo $f | grep -oEi '[0-9]+_([a-z]+)_[0-9a-z]*' name=$? echo $name done So far $name merely holds 0, 1 or 2,…
Isaac
  • 15,783
  • 9
  • 53
  • 76
494
votes
12 answers

How can I grep for a string that begins with a dash/hyphen?

I want to grep for the string that starts with a dash/hyphen, like -X, in a file, but it's confusing this as a command line argument. I've tried: grep "-X" grep \-X grep '-X'
Mike
  • 58,961
  • 76
  • 175
  • 221
486
votes
3 answers

How can I make grep print the lines below and above each matching line?

I want to search each line for the word FAILED, then print the line above and below each matching line, as well as the matching line. Input: id : 15 Status : SUCCESS Message : no problem id : 15 Status : FAILED Message : connection error Desired…
poiuytrez
  • 21,330
  • 35
  • 113
  • 172
448
votes
7 answers

Get line number while using grep

I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are print on the terminal. But is it possible to get the line numbers of those lines too?? ex: presently what I get is…
sai
  • 4,907
  • 4
  • 24
  • 18
430
votes
8 answers

How can I format my grep output to show line numbers at the end of the line, and also the hit count?

I'm using grep to match string in a file. Here is an example file: example one, example two null, example three, example four null, grep -i null myfile.txt returns example two null, example four null, How can I return matched lines together with…
London
  • 14,986
  • 35
  • 106
  • 147
425
votes
16 answers

How do I grep for all non-ASCII characters?

I have several very large XML files and I'm trying to find the lines that contain non-ASCII characters. I've tried the following: grep -e "[\x{00FF}-\x{FFFF}]" file.xml But this returns every line in the file, regardless of whether the line…
pconrey
  • 5,805
  • 7
  • 29
  • 38
411
votes
11 answers

How to check if a file contains a specific string using Bash

I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work: if [[ 'grep 'SomeString' $File' ]];then # Some Actions fi What's wrong in my code?
Hakim
  • 11,110
  • 14
  • 34
  • 37
384
votes
11 answers

Fast way of finding lines in one file that are not in another?

I have two large files (sets of filenames). Roughly 30.000 lines in each file. I am trying to find a fast way of finding lines in file1 that are not present in file2. For example, if this is file1: line1 line2 line3 And this is…
Niels2000
  • 4,027
  • 3
  • 16
  • 15
371
votes
5 answers

How to perform grep operation on all files in a directory?

Working with xenserver, and I want to perform a command on each file that is in a directory, grepping some stuff out of the output of the command and appending it in a file. I'm clear on the command I want to use and how to grep out string(s) as…
user2147075
  • 3,711
  • 2
  • 13
  • 3
353
votes
10 answers

How to search and replace using grep

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string. I know that the command to find it might look like this: grep 'string_to_find' -r ./* But how…
billtian
  • 6,589
  • 4
  • 18
  • 28