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
1 answer

syntax error near unexpected token `(' in bash

I want to use grep and a regular expression to search a text document. When I type in this: grep -o ((D|d)ie|(D|d)as|(D|d)e(r|n|m|s)|(ei|Ei)(n|ne|nen|nem|ner|nes)) [A-ZÄÖÜ][A-Za-zäöü]* document.txt I get this: -bash: syntax error near unexpected…
bogdan
  • 97
  • 1
  • 7
4
votes
5 answers

Extract value from xml in bash on a mac

I need to extract the name value (Product Finder) from this xml: File: config.xml
mles
  • 4,534
  • 10
  • 54
  • 94
4
votes
3 answers

SED and GREP showing different results

I'm trying to get the amount of requests in a specific range of time from my Apache log. I though it was quite easy doing that with sed however when I tried doing the same with grep I realised that grep shows more results than sed. Here's the grep…
Adonist
  • 153
  • 1
  • 6
4
votes
2 answers

grep multiple hits in one line (bash)

I need to find multiple hits in one line (using grep/egrep). Specifically, as an example, I need to find all words surrounding the word "und" in the text "Geschehnis und Beispiel und Grund und Ursachen". But egrep "\w+ und \w+" has only two, not…
dia
  • 329
  • 2
  • 12
4
votes
2 answers

Find files that are too long for Synology encrypted shares

When trying to encrypt the homes share after the DSM6.1 update, I got a message that there are files with filenames longer than 143 characters. This is the maximum length for a filename in an encrypted Synology share. Because there is a lot of stuff…
Marlon
  • 321
  • 2
  • 8
4
votes
3 answers

awk and equivalence classes

Does gnu awk support POSIX equivalence classes? Is it possible to match [[=a=]] using awk as it is done in grep? $ echo ábÅ | grep [[=a=]] ábÅ $ echo ábÅ | grep -o [[=a=]] á Å
Eugene Barsky
  • 5,780
  • 3
  • 17
  • 40
4
votes
7 answers

extract a quoted value from multiple lines using sed

I'm a total n00b when it comes to using sed and can't quite figure this out... I've got the following data being output by a command line tool: ruby: interpreter: "ruby" version: "1.8.6" date: "2010-02-04" platform: …
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
4
votes
3 answers

C# Programming How to grep columns/lines from Text File?

I have a C# console program which main functions should let a user grep lines / columns from a log text file. An Example within the text file the user wishes to grep a group of all the related lines starting from a particular date etc. "Tue Aug 03…
JavaNoob
  • 3,494
  • 19
  • 49
  • 61
4
votes
2 answers

How can I match the lowercase version of a backreference

I'd like to match the lowercase version of an uppercase character in a backreference in a regex. For example, let's say I want to match a string where the 1st character is any uppercase character and the 4th character is the same letter as the first…
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
4
votes
4 answers

How to grep from a single line

I'm using a weather API that outputs all data in a single line. How do I use grep to get the values for "summary" and "apparentTemperature"? My command of regular expressions is basically nonexistent, but I'm ready to…
user1901162
  • 1,017
  • 1
  • 8
  • 8
4
votes
2 answers

Is silver-searcher able to obtain PATTERN from file?

There are 84 PATTERN need to be check, i store them in file name pattern.txt. Is silver-searcher (also named Ag) able to obtain these patterns from pattern.txt? grep has -f options to read pattern from file, but the man page of silver-searcher…
good5dog5
  • 139
  • 1
  • 2
  • 10
4
votes
2 answers

Character offset with grep

When I run this on my bash terminal: grep -ob "amilase" <<< "α-amilase" I get this: 3:amilase Let's define byte offset as the number of bytes before the matching word and character offset as the number of user-visible characters before the…
LLCampos
  • 295
  • 4
  • 17
4
votes
5 answers

grep array parameter of excluded files

I want to exclude some files from my grep command, for this I'm using parameter: --exclude=excluded_file.ext To make more easy to read I want to use a bash array with excluded files: EXCLUDED_FILES=("excluded_file.ext") Then pass ${EXCLUDED_FILES}…
0x3d
  • 460
  • 1
  • 8
  • 27
4
votes
2 answers

git grep showing absolute paths

I would like to use git grep from outside the repository, for example editing a file in vim. This works: git -C /path/to/gitrepo/ grep 'my search' The problem is that the results show relative path from /path/to/gitrepo. So, I cannot open them…
phcerdan
  • 730
  • 7
  • 16
4
votes
5 answers

How to completely erase the duplicated lines by linux tools?

This question is not equal to How to print only the unique lines in BASH? because that ones suggests to remove all copies of the duplicated lines, while this one is about eliminating their duplicates only, i..e, change 1, 2, 3, 3 into 1, 2, 3…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144