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

grep:how to use non-capturing group?

I am confused about the following command $ cat num.txt 1 2 3 1st 2nd 3th $ cat num.txt | grep -Eo '[0-9](?:st|nd|th)?' I think it should output as 1 2 3 1 2 3 But it output as 1 2 3 1 2nd 3th What am I doing wrong here?Thanks for any…
Ren
  • 2,852
  • 2
  • 23
  • 45
4
votes
2 answers

How to index text files to improve grep time

I have a large number of text files I need to grep through on a regular basis. There are ~230,000 files amounting to around 15GB of data. I've read the following threads: How to use grep efficiently? How to use grep with large (millions) number of…
Richard Horrocks
  • 419
  • 3
  • 19
4
votes
1 answer

How to grep for exact hexadecimal value of characters

I am trying to grep for the hexadecimal value of a range of UTF-8 encoded characters and I only want just that specific range of characters to be returned. I currently have this: grep -P -n "[\xB9-\xBF]" $str_st_location >> output_st.txt But this…
user2056389
  • 105
  • 2
  • 6
4
votes
1 answer

Logical OR in GLOB during grep --include/--exclude

In the Ubuntu terminal, I would like to grep all files with (or excluding) extension .foo and .bar for the phrase 'foobar'. I've read this advice for creating a GLOB with a logical or, and tried a few combinations but none seem to work: rgrep…
LondonRob
  • 73,083
  • 37
  • 144
  • 201
4
votes
1 answer

How do I convert from using `grep -P` to `grep -E`?

I have a script that we use on Ubuntu (Linux) and I'd like to convert it to be used on both Ubuntu (Linux) and MacOS X. grep on Linux is different than grep on FreeBSD (i.e. MacOS X); grep on MacOS X doesn't support the -P option. Unfortunately,…
Son of the Wai-Pan
  • 12,371
  • 16
  • 46
  • 55
4
votes
1 answer

How to grep the logs between two time range

I have a log file abc.log in which content is like this when I do a vi on it. 2015-05-28 18:12:00,815 INFO 0x7f6ed23e3700 send_to - recent: 1432861920 from dc:2 client_id: 102 2015-05-28 18:13:01,024 INFO 0x7f6ed23e3700 send_to - recent: 1432861980…
john
  • 11,311
  • 40
  • 131
  • 251
4
votes
6 answers

(bash) check if IP in subnet range file

I've a list of subnet range in a file: 2.32.0.0-2.47.255.255-255.240.0.0 2.112.0.0-2.119.255.255-255.248.0.0 2.156.0.0-2.159.255.255-255.252.0.0 2.192.0.0-2.199.255.255-255.248.0.0 ... (The file format is: {startip}-{endip}-{netmask}) I need check…
Stefano Guarella
  • 59
  • 1
  • 1
  • 6
4
votes
4 answers

Replace every n'th occurrence in huge line in a loop

I have this line for example: 1,2,3,4,5,6,7,8,9,10 I want to insert a newline (\n) every 2nd occurrence of "," (replace the 2nd, with newline) .
ocra88
  • 103
  • 1
  • 7
4
votes
1 answer

How to read from STDIN in python from a piped grep output

Let's say I have a text file and grep "@" file.txt returns: (1) preparing corpus @ Tue Apr 28 20:19:31 CEST 2015 (1.0) selecting factors @ Tue Apr 28 20:19:31 CEST 2015 (1.2) creating vcb file…
alvas
  • 115,346
  • 109
  • 446
  • 738
4
votes
1 answer

git grep in only untracked files

I would like to grep inside Untracked files and in files from Changes not staged for commit but not in the rest. Is it possible to achieve this with git grep ?
Charles
  • 11,367
  • 10
  • 77
  • 114
4
votes
1 answer

BASH: Search for several Keywords in file and highlight them

I want to search all files in a directory for a Keyword and color it, like: grep -n "keyword1" * --color Now I want to highlight all occurrences of keyword2 and keyword3 in an other color. I managed grep -n "keyword1 * |egrep --color…
meister_reineke
  • 364
  • 1
  • 14
4
votes
2 answers

Regex to find external links from the html file using grep

From past few days I'm trying to develop a regex that fetch all the external links from the web pages given to it using grep. Here is my grep command grep -h -o -e "\(\(mailto:\|\(\(ht\|f\)tp\(s\?\)\)\)\://\)\{1\}\(.*\?\)"…
Amar
  • 6,874
  • 4
  • 25
  • 23
4
votes
7 answers

Search .netrc file in bash, and return username for given machine

I need a way (the most portable) in bash, to perform a search of the ~/.netrc file, for a particular machine api.mydomain.com and then on the next line, pull the username value. The format is: machine a.mydomain.com username foo passsword…
Justin
  • 42,716
  • 77
  • 201
  • 296
4
votes
1 answer

Deviant grep behavior for Inverse matching

I have a file containing identifiers (one per line) that I wish to run through an unspecified analysis. The analysis will, if everything runs perfectly, output another list with the same identifiers (not necessarily in the same order). However, it…
EKarl
  • 149
  • 1
  • 11
4
votes
3 answers

Change a configuration variable or add it if it does not exist using sed/awk/grep

I have a configuration file located at /etc/ssh/sshd_config that may or may not include a configuration line that sets PermitRootLogin. PermitRootLogin yes My goal is to either change this value to something I know is correct (regardless of the…
user636044
1 2 3
99
100