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

Apply substitution to the first item returned by grep

I have this program sub f { …
Patrik
  • 845
  • 2
  • 8
  • 20
4
votes
4 answers

grep serial numbers not starting with specific prefix

I have this file (serials.txt) containing serial numbers: S/N:175-1915011190 S/N:244-1920023447 S/N:335-1920101144 S/N:244-1920101149 Using grep or similar tool I want to select all serials NOT starting with '244' I'm able to select all the '244'…
SBF
  • 1,252
  • 3
  • 12
  • 21
4
votes
2 answers

Getting whitespace characters from grep into an array

Where I started: input="a s d f" content=(`grep -o . <<< "$input"`) echo ${#content[@]} for ((a = 0; a < ${#content[@]}; a++)); do token="${content[a]}" echo "$token" done read -p '' echoes: 4 a s d f Although the grep command is capturing…
Kirk Walek
  • 53
  • 5
4
votes
1 answer

Echo multiline var to Grep

How do you pipe a multiline variable to grep and retain the newlines? Expected results: $ git status --porcelain --branch ## test-branch M image.go M item.go ?? addme.go ?? testdata/ $ git status --porcelain --branch | grep -c '^??' 2 "2" is the…
eduncan911
  • 17,165
  • 13
  • 68
  • 104
4
votes
3 answers

I want grep to grep one word which is having spaces it

I would like to grep the word “s a i” (which has spaces in it) in a xyz.txt file which is saved in another directory. I tried to find an answer but I didn’t manage to find any. I have to use the grep command only.
Sai Srikanth
  • 41
  • 1
  • 1
  • 4
4
votes
4 answers

grep range of numbers

For the following text I need to count the lines that the number between parenthesis is in the range [10-20]. This function's cyclomatic complexity is too high. (1) This function's cyclomatic complexity is too high. (2) This function's…
Rashomon
  • 5,962
  • 4
  • 29
  • 67
4
votes
3 answers

BASH: Find number in text -> variable

I need little help from the community: I have these two lines in a large text file: Connected clients: 42 4 ACTIVE CLIENTS IN LAST 20 SECONDS How I can find, extract and assign the numbers to variables? clients=42 active=4 SED, AWK, GREP?…
Adrian
  • 115
  • 1
  • 2
  • 5
4
votes
5 answers

How to replace one character inside parentheses keep everything else as is

The data looks like this : There is stuff here (word, word number phrases) (word number anything, word phrases), even more ... There is a lot of them in different files. There is different kind of data too, all around it that isn't in the same…
4
votes
1 answer

Distinct quickfix buffers for vimgrep and make

When make-ing under Vim, there is often a need to vimgrep the files. In such cases, vimgrep takes over the quickfix buffer, so one needs to re-make in order to browse remaining compiler errors. Is there a way to avoid this mess?
Paul Oyster
  • 1,228
  • 6
  • 16
  • 19
4
votes
2 answers

Grepping for exact string while ignoring regex for dot character

So here's my issue. I need to develop a small bash script that can grep a file containing account names (let's call it file.txt). The contents would be something like this: accounttest account2 account accountbtest account.test Matching an exact…
Tony
  • 460
  • 1
  • 7
  • 20
4
votes
2 answers

Find all accented words (diacriticals) using grep?

I have a large list of words in a text file (one word per line) Some words have accented characters (diacriticals). How can I use grep to display only the lines that contain accented characters?
R OMS
  • 652
  • 2
  • 7
  • 19
4
votes
1 answer

grep on a single file several times

I have a file with accession numbers. Those numbers needs to be mapped against IDs into another file, and with that information and complementary mysql database information write a third file. I have a simple program which reads the file (145Gb),…
jcoder8
  • 163
  • 1
  • 9
4
votes
2 answers

grep pipe with sed

This is my bash command grep -rl "System.out.print" Project1/ | xargs -I{} grep -H -n "System.out.print" {} | cut -f-2 -d: | sed "s/\(.*\):\(.*\)/filename is \1 and line number is \2/ What I'm trying to do here is,I'm trying to iterate…
123Ex
  • 237
  • 2
  • 5
  • 9
4
votes
4 answers

How to reverse the output of the grep command?

i have one problem with grep command. in my called script i use: "ServicePassword":fooooooooooo "ServiceUserName":barrrrrrrrr grep -E '"ServiceUserName"|"ServicePassword"' >> user.txt but in user.txt file i will get first…
Андрей Ка
  • 756
  • 4
  • 14
  • 33
4
votes
3 answers

Grep or the like: overlapping matches

For: echo "the quick brown fox" | grep -Po '[a-z]+ [a-z]+' I get: the quick brown fox but I wanted: the quick quick brown brown fox How?
Adrian May
  • 2,127
  • 15
  • 24