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

Count total directories and files in all sub directories unix

I am trying to get a count of the subdirectories and all the files total in all the subdirectories within a directory in Unix. I tried this: ls -lR | grep ^d | wc -l but that just gives me the total subdirectories, not the total files. How would…
Will
  • 99
  • 1
  • 8
4
votes
3 answers

How to grep rows that have value less than or equal to value

size=139 size=9292 size=140 size=139 size=139 size=10213 size=139 size=140 size=11186 size=139 size=139 size=140 size=12342 size=139 size=139 size=140 size=11360 How can I grep rows that has greater value than 1000 from size.txt ? Also, is there…
Ben
  • 91
  • 1
  • 2
  • 9
4
votes
1 answer

Missing grep filter in logstash

I'm trying to use the grep function, but it not works, this is the error : Couldn't find any filter plugin named 'grep'. Are you sure this is correct? Trying to load the grep filter plugin resulted in this error: no such file to load --…
Morito
  • 93
  • 2
  • 13
4
votes
1 answer

UNIX grep command (grep -v grep)

I was going through something and found this which I could not understand, grep -v grep What does this signify? I know that -v switch will select all the lines that do not match. But why the second grep? This is the full command: ps -ef | grep rsync…
Dhiren Dash
  • 111
  • 1
  • 10
4
votes
2 answers

grep current directory only

I would like to search all the files in the current directory only. I tried this grep foo * but I get this error grep: bar: Is a directory I also tried this grep -r foo but this is searching subdirectories as well.
Zombo
  • 1
  • 62
  • 391
  • 407
4
votes
3 answers

Find lines starting with one specific character and ending with another one

I need to find a line in a file that starts with an a and the last word in the line ends with an e. How can I do it with a tool like grep?
PFKrang
  • 229
  • 2
  • 7
  • 17
4
votes
1 answer

Regex match reverse of a group in a string

I need to match a group at the start of a string with the reverse of group at the end. Ex: batmantab would match because bat and tab. racecar would match because rac and car. current regex: '^(\w{3})\w*\1$' Any Suggestions?
Jim Thome
  • 61
  • 1
  • 2
4
votes
2 answers

BASH: Finding maximum value in a particular CSV column

I have a CSV file million_songs_metadata_and_sales.csv having the following schema. track_id sales_date sales_count title song_id release artist_id artist_mbid artist_name duration artist_familiarity artist_hotttnesss year Sample…
AngryPanda
  • 1,261
  • 2
  • 19
  • 42
4
votes
2 answers

Grep files in between wget recursive downloads

I am trying to recursively download several files using wget -m, and I intend to grep all of the downloaded files to find specific text. Currently, I can wait for wget to fully complete, and then run grep. However, the wget process is time consuming…
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
4
votes
4 answers

How to grep lines starting with a digit or white space

I need to count messages per hour in my log file. Every log file line is preceded by the time stamp. Hence I am using following 'for' and 'grep' command to do this - for i in `seq 0 23` do egrep "$i:[0-9][0-9]:[0-9][0-9] " filename |…
punekr12
  • 653
  • 2
  • 7
  • 14
4
votes
5 answers

awk solution for pattern matching and allowing one ambiguity/mismatch

I would like to count the number of strings in a document. If input is: GGTGGTGGTAT GGTAGTGGTAT GGTGGTGGTAT GGTAATGGTAT And I search for GGTGGTGGT I would like to find 3 matches. Allowing for one ambiguity. Using egrep it would look something…
Stuber
  • 447
  • 5
  • 16
4
votes
3 answers

concatenate grep output to an echo statement in UNIX

I am trying to output the number of directories in a given path on a SINGLE line. My desire is to output this: X-many directories Currently, with my bash sript, I get this: X-many directories Here's my code: ARGUMENT=$1 ls -l $ARGUMENT | egrep…
Rumen Hristov
  • 867
  • 2
  • 13
  • 29
4
votes
1 answer

how to pass values from stdout as parameter for the next command

i want to svn blame lines of code which include "todo | fixme" i have the general flow of the script but struggle to combine it into one finding the lines with "todo" grep --color -Ern --include=*.{php,html,phtml} --exclude-dir=vendor…
braunbaer
  • 1,113
  • 1
  • 9
  • 20
4
votes
3 answers

Perform grep on individual results of a command spawned by xargs

Is it possible to grep the result of a command spawned by xargs? As an example I am trying the following command findbranch prj-xyz -latest|sed 's/^\(.*\/.*\)@@.*$/\1/'|xargs -I {} cleartool lsh {}|grep -m 1 'user' but seems like grep is executing…
Abhijit
  • 62,056
  • 18
  • 131
  • 204
4
votes
3 answers

Optimizing grep (or using AWK) in a shell script

In my shell script, I am trying to search using terms found in a $sourcefile against the same $targetfile over and over. My $sourcefile is formatted as such: pattern1 pattern2 etc... The inefficient loop I have to search with is: for line in $(<…
Ode
  • 465
  • 1
  • 5
  • 12
1 2 3
99
100