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
4
votes
3 answers

Grep words before and after match?

Is there a simple solution to also extract one word before and one word after along with the matched word? For example, assuming the following text ... put returns between paragraphs ... ... the function returns void ... the search for returns…
user3639557
  • 4,791
  • 6
  • 30
  • 55
4
votes
2 answers

How to specify ignore pattern in grep?

How can I make grep to ignore some files (e.g. .svn directories, binary files, etc.) while grepping recursively?
Vahagn
  • 4,670
  • 9
  • 43
  • 72
4
votes
4 answers

Bash script counting instances of itself wrongly

I've created a bash script which counts launched instances of itself. Here it is (in this example, I'm showing the instances rather than counting them with wc -l) : #!/bin/bash nb=`ps -aux | grep count_itself.sh` echo "$nb" sleep 20 (Of course, my…
roberto06
  • 3,844
  • 1
  • 18
  • 29
4
votes
3 answers

Filter lines with the other set of lines in Bash

I have lines on my standard input $printf "C\nB\nA\n" C B A and I want to filter out lines (or substrings or regexps - whatever is easier) that appear on some other standard input: $printf "B\nA\n" B A I expect just C when entries get…
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
4
votes
1 answer

Parse through two files and concatenate strings into one file using bash

I have two text files which I would like to parse through, then combine the two lines parsed into one file using a grep call. For example: Foo.txt Foo2.txt *1 A 2 B *3 …
branch.lizard
  • 595
  • 1
  • 5
  • 15
4
votes
3 answers

find - grep taking too much time

First of all I'm a newbie with bash scripting so forgive me if i'm making easy mistakes. Here's my problem. I needed to download my company's website. I accomplish this using wget with no problems but because some files have the ? symbol and…
4
votes
1 answer

Using grep to compare two lists of Python packages

I want to generate the list of packages install in Python 3, the list of all packages in Python 2.7 and find all entries in the 2.7 list not in the Python 3 list. Generating the list is easy: pip freeze or pip3.4 freeze. Searching for a package in…
davidhood2
  • 1,367
  • 17
  • 47
4
votes
4 answers

Bash : Check if file contains other file contents

So I'm trying to append the contents of one file to another file, if it's not already included. This is how I try: catAndAppendIfMissing(){ [[ ! -s $2 ]] && touch "$2" || [[ ! -s $2 ]] && sudo touch "$2" if grep $1 $2; then echo…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
4
votes
7 answers

Text specification for a tree of files?

I'm looking for examples of specifying files in a tree structure, for example, for specifying the set of files to search in a grep tool. I'd like to be able to include and exclude files and directories by name matches. I'm sure there are examples…
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
4
votes
1 answer

tail & grep file and uniquely color by IP addresses

I'm stuck on trying to colorize tail -f output so that the IP address color is unique per IP address. I can't find anything by searching. Here's some code that colors the current IP address alone but it's the same color for every IP. tail -f…
Jack
  • 3,271
  • 11
  • 48
  • 57
4
votes
3 answers

Regex to match unique substrings

Here's a basic regex technique that I've never managed to remember. Let's say I'm using a fairly generic regex implementation (e.g., grep or grep -E). If I were to do a list of files and match any that end in either .sty or .cls, how would I do…
Will Robertson
  • 62,540
  • 32
  • 99
  • 117
4
votes
2 answers

why is ^A|bA slower than negative lookbehind with grep -P, but fast with grep -E?

These are equivalent: grep -E '^A|bA' grep -P '^A|bA' grep -P '(?
mgiuffrida
  • 3,299
  • 1
  • 26
  • 27
4
votes
2 answers

Why does my grep command output "--" between some lines?

I have a fasta file like the test one here: >HWI-D00196:168:C66U5ANXX:3:1106:16404:19663 1:N:0:GCCAAT CCTAGCACCATGATTTAATGTTTCTTTTGTACGTTCTTTCTTTGGAAACTGCACTTGTTGCAACCTTGCAAGCCATATAAACACATTTCAGATATAAGGCT >HWI-D00196:168:C66U5ANXX:3:1106:16404:19663…
michberr
  • 303
  • 1
  • 10
4
votes
1 answer

Bash script process name regex

I'm trying to regex process id's based on parts of a process name. It seems to work if I only do a single word, but it fails when I try to do something like: find me any process with path /beginning ** /endswiththis/ Here's what I have so…
Steve
  • 341
  • 1
  • 4
  • 9
4
votes
5 answers

How do I filter tab-separated input by the count of fields with a given value?

My data(tab separated): 1 0 0 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 ... how can i grep the lines with exact, for example, 5 '1's, ideal output: 1 0 0 …
once
  • 1,369
  • 4
  • 22
  • 32