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

How to escape double dollars in a makefile?

Consider a directory containing (only) the 3 files obtained by: echo "foobar" > test1.txt echo "\$foobar" > test2.txt echo "\$\$foobar" > test3.txt (and thus containing respectively foobar, $foobar, $$foobar). The grep instruction: grep -l -r…
Denis Bitouzé
  • 520
  • 6
  • 18
4
votes
1 answer

Why is Gnu grep exit status always 0 on standard input?

On a bash command line in Ubuntu 14.04 : echo "z" | grep -e 'x' ; echo $PIPESTATUS always displays 0, even though, obviously, there's no "x" in "z". But: echo "z" > /tmp/z grep -e 'x' /tmp/z ; echo $PIPESTATUS works as I expect, returning 1 if…
4
votes
4 answers

bash grep regexp - excluding subpattern

I have a script written in bash, with one particular grep command I need to modify. Generally I have two patterns: A & B. There is a textfile that can contain lines with all possible combinations of those patterns, that is: "xxxAxxx", "xxxBxxx",…
ptr92zet
  • 173
  • 9
4
votes
3 answers

How I can sort a text file for specific string?

I have a text file with the following lines: Ca4 0.500001 0.000000 0.000000 C4 0.750001 0.500000 0.000000 O10 0.750001 0.243180 0.000000 O8 0.652432 0.628410 -0.779621 O12 0.847569 0.628410 0.779621 Ca3 0.120090 0.500000 -3.035668 C3 …
git
  • 151
  • 9
4
votes
5 answers

Replace nth line below the searched pattern in a file

I needed to replace 4th line (which contain pattern Y) after a search pattern (say X) in a file. Please note pattern Y is used elsewhere too. So I can't replace it directly. Also note on the 4th line there is only one word Y. Is there any scripting…
amrendra47
  • 49
  • 1
  • 6
4
votes
1 answer

Why [^\d\w\s,] matches "leonardo,davinci"?

I can't understand why the regexp: [^\d\s\w,] Matches the string: "leonardo,davinci" That is my test: $ echo "leonardo,davinci" | egrep '[^\d\w\s,]' leonardo,davinci While this works as expected: $ echo "leonardo,davinci" | egrep '[\S\W\D]' $…
Luca
  • 801
  • 1
  • 6
  • 11
4
votes
1 answer

call to system() does not give expected output in C

I wrote this code to check if any process with the name Rahul is running or not: int ret = -1; char cmd[40]="pgrep -f Rahul > /dev/null"; ret = WEXITSTATUS(system(cmd)); printf("%d\n", ret); This code prints 0 indicating that a process…
4
votes
5 answers

process a text file using various delimiters

My text file (unfortunately) looks like this... [amar-1000#Fem$$$_Y](1){india|1000#Fem$$$,mumbai|1000#Mas$$$} [akbar-1000#Fem$$$_Y](1){} [-0000#$$$_N](0){USA|0100#$avi$$,NJ|0100#$avi$$} It contain the customer name followed by…
shantanuo
  • 31,689
  • 78
  • 245
  • 403
4
votes
1 answer

How to redirect grep to a while loop

Hi I have the following bash script code group2=0 while read -r line do popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c if [[ $popAll = 0 ]]; then group2 = $((group2+2)); else …
Kyriakos
  • 757
  • 8
  • 23
4
votes
2 answers

Why do you have to escape | and + in grep between apostrophes?

I was under the impression that within single quotes, e.g. 'pattern', bash special characters are not interpolated, so one need only escape single quotes themselves. Why then does echo "123" | grep '[0-9]+' output nothing, whereas echo "123" | grep…
Kev
  • 15,899
  • 15
  • 79
  • 112
4
votes
2 answers

grep shows occurrences of pattern on a per line basis

From the input file: I am Peter I am Mary I am Peter Peter Peter I am Peter Peter I want output to be like this: 1 I am Peter 3 I am Peter Peter Peter 2 I am Peter Peter Where 1, 3 and 2 are occurrences of "Peter". I tried this, but the info is…
Cindy Turlington
  • 2,456
  • 3
  • 15
  • 20
4
votes
1 answer

Grep: Recursive option produces unexpected behavior when fed pipe-input

I've been using this utility successfully for many years, in many environemnts. But I'm noticing that on one particular environment, it produces very unexpected results. grep -r 'search-term1' . | grep 'search-term2' The above code greps…
RvPr
  • 1,074
  • 1
  • 9
  • 26
4
votes
2 answers

Extracting MAC Address from arp table using grep regex

I am trying to extract MAC address from arp table without but it is returning a empty line. I tried to use the command: arp -a | grep eth1 | grep '^\A(([0-9a-fA-F]{2}):){5}([0-9a-fA-F]{2})$\z' The 'arp -a' command return: ? (192.168.36.20) at…
Ricardo
  • 667
  • 9
  • 25
4
votes
3 answers

Bash function to process all dotfiles in a directory excluding directories

I am looking to create a bash function to filter all the dotfiles (no directories) in a selected directory. I only need the file name, not the full path. For the moment, i only have this command: find . -maxdepth 1 -type f -print0 which prints all…
Jeanmichel Cote
  • 531
  • 1
  • 5
  • 19
4
votes
2 answers

grep multiple patterns single file argument list too long

I am currently searching for multiple patterns in a file. The file is of 90GB in size, I am searching on a particular field(from position 6-17 in each line). I am trying to get all the lines that contain any of a particular list of numbers. The…
reddy
  • 180
  • 1
  • 8