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 parse contents of a file using sed/awk?

My input file has its content in following format, where each column is separated by a "space" string1string2string3YYYY-mm-ddhh:mm:ss.SSSstring410:12345678900e:Apple…
intruder
  • 417
  • 1
  • 3
  • 18
4
votes
4 answers

filter with grep to print when text is NOT present in another file

I have a linux filter to extract all lines from an xcode project that contain localized strings and produce a sorted list of unique entries. The filter works fine and is shown below. grep NSLocalized *.m | perl -pe…
Roger
  • 15,793
  • 4
  • 51
  • 73
4
votes
3 answers

How may I replace multiple delimited strings of a line non-greedily?

I want to generate riddles from accordingly formatted input strings. Example input: Foo was the +first+ to get a drink at +the bar+. Desired output: Foo was the _____ to get a drink at ___ ___. With any standard shell tool, what's the simplest (on…
user569825
  • 2,369
  • 1
  • 25
  • 45
4
votes
4 answers

Extract string under a string from a file in linux

This site have helped me a lot in the past but signed up only now as I couldn't find an exact answer for what am trying now. I hope you would be able to help. I'm trying to extract or cut the values under a string in a file. My file looks…
Ribs
  • 53
  • 1
  • 4
4
votes
5 answers

How do I find broken NMEA log sentences with grep?

My GPS logger occassionally leaves "unfinished" lines at the end of the log files. I think they're only at the end, but I want to check all lines just in case. A sample complete sentence looks…
Tom
  • 42,844
  • 35
  • 95
  • 101
4
votes
4 answers

How can I grep content from file1 matching file2 and put them in the order of file2

I have file1.txt with content: rs002 rs113 rs209 rs227 rs151 rs104 I have file2.txt with content: rs113 113 rs002 002 rs227 227 rs209 209 rs104 104 rs151 151 I want to get the lines of file2.txt that match the records in file1.txt,…
4
votes
3 answers

How to make grep separate output by NULL characters?

Suppose we are doing a multiline regex pattern search on a bunch of files and we want to extract the matches from grep. By default, grep outputs matches separated by newlines, but since we are doing multiline patterns this creates the inconvenience…
chtenb
  • 14,924
  • 14
  • 78
  • 116
4
votes
2 answers

How awk the filename as a column in the output?

I am trying to perform some grep in contents of several files in a directory and appending my grep match in a single file, in my output I would also want a column which will have the filename as well to understand from which files that entry was…
ivivek_ngs
  • 917
  • 3
  • 10
  • 28
4
votes
1 answer

Finding strings with a specific number of letters, extensions included with grep

If I pipe ls to grep, how would I be able to return a list of files with the condition that they only have an x amount of letters, extension included? So for example, if ls gives me: abcde.jav a156e.exc test.c prog1.c qwert r.c and I'm looking…
nsaftarli
  • 149
  • 1
  • 1
  • 10
4
votes
2 answers

Unix: How can I count all lines containing a string in all files in a directory and see the output for each file separately

In UNIX I can do the following: grep -o 'string' myFile.txt | wc -l which will count the number of lines in myFile.txt containing the string. Or I can use : grep -o 'string' *.txt | wc -l which will count the number of lines in all .txt extension…
Ravid Goldenberg
  • 2,119
  • 4
  • 39
  • 59
4
votes
2 answers

Redirecting output of list of commands

I am using grep to pull out lines that match 0. in multiple files. For files that do not contain 0., I want it to output "None" to a file. If it finds matches I want it to output the matches to a file. I have two example files that look like this: $…
Batwood
  • 91
  • 7
4
votes
3 answers

How to combine multiple grep commands?

I have a long .txt file (LONG.txt). In that txt file, I want to search for 3 types of patterns and then I want to capture the grep result into a new txt file (SHORT.txt). Patterns: AAAAA BBBBB CCCCC NOTE: When pattern AAAAA or BBBBB is found, I…
Jatin
  • 1,857
  • 4
  • 24
  • 37
4
votes
3 answers

RegEx for finding words with NOT ONLY ONE space between them

I need help with a RegEx problem: I want to find occurences of two known words ("foo" and "bar" for example), that have any white space other than EXACTLY ONE SPACE CHARACTER between them. In the text that I have to grep, there may be spaces, tabs,…
selfawaresoup
  • 15,473
  • 7
  • 36
  • 47
4
votes
3 answers

shell regex: Extract prices

Given the following list of prices, I am trying to figure out how to normalize/extract only the digits. INPUT DESIRED_OUTPUT CA$1399.00 1399.00 $1399.11 1399.11 $1,399.22< 1399.22 Z$1 399.33 1399.33 $1399.44# …
octosquidopus
  • 3,517
  • 8
  • 35
  • 53
4
votes
3 answers

In Bash, how to write alias or function for "export GREP_COLOR='1;32'; grep --color"?

grep doesn't allow setting color by grep --color='1;32' (1 meaning bold, and 32 meaning green). It has to use GREP_COLOR by export GREP_COLOR='1;32' and then use grep --color How do we alias or write a function for grep so that we have 2…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740