Questions tagged [grep]

grep is a Unix command-line utility that searches input for lines matching (or not) a regular expression and prints the results.  Use this tag for questions about how to use grep features, how to write regular expressions that work with grep, and general usage.  Because grep is now available in Unix-like environments (e.g., Cygwin on Windows), you can also use this tag for compatibility questions, and differences between versions and software environments.

grep is a Unix command-line utility that searches input passed to it for lines (or NUL-terminated records) matching (or not matching) a regular expression (or regular expressions), and prints the results.  As is typical for Unix command-line utilities, it can take input from files and/or read the standard input.

Usage

Use this tag for questions about how to use grep features, how to write regular expressions that work with grep, and general usage.  Because grep is now available in Unix-like environments (including Cygwin on Windows), you can also use this tag for compatibility questions, and differences between versions and software environments.

Related tags

References

1106 questions
287
votes
5 answers

Getting colored results when using a pipe from grep to less

I use the --colour option of grep a lot, but I often use less as well. How can I pipe grep results to less and still preserve the coloring. (Or is that possible?) grep "search-string" -R * --colour | less EDIT: I'm looking for a direct solution…
Jeremy Powell
  • 6,799
  • 5
  • 23
  • 16
287
votes
14 answers

Grep equivalent for Windows 7?

Is there a command prompt grep equivalent for Windows 7? That is, I want to filter out the results of a command: Bash use: ls | grep root What would it be from a Windows command prompt?
chrisjlee
  • 3,980
  • 4
  • 22
  • 27
224
votes
4 answers

Using watch with pipes

I'd like to run this command: watch -n 1 tail -n 200 log/site_dev.log | grep Doctrine But it does not run, because "I think" that the grep tries to run on the watch instead of the tail... Is there a way to do something like watch -n 1 (tail -n 200…
Tommy B.
  • 2,589
  • 3
  • 19
  • 17
92
votes
8 answers

How to find uptime of a linux process

How do I find the uptime of a given linux process. ps aux | grep gedit | grep -v grep gives me a whole lot of information which includes the time at which the process was started. I am specifically looking for switch which returns the uptime of a…
Mahadevan Sreenivasan
  • 1,023
  • 1
  • 8
  • 6
90
votes
9 answers

grep to find files that contain ^M (Windows carriage return)

I use Linux. There is a pesky ^M (Windows cariage return) somewhere hidden in thousands of configuration files, and I have to find it, because it makes the server fail. How do I find ^M among a directories hierarchy full of configuration files? I…
Nicolas Raoul
  • 10,711
  • 18
  • 64
  • 102
88
votes
3 answers

How to use 'sed' with piping

I want to replace a string outputted from grep, I have: $ npm info webpack | grep version it outputs me $ version: '2.1.0-beta.12', but I want to have: $ 2.1.0-beta.12 So I think I might achieve that using sed and replace unnecessary substrings.…
Oskar Szura
  • 1,005
  • 1
  • 7
  • 8
84
votes
7 answers

Display all output but highlight search matches

In bash, if I want to execute a command and only display output lines that matches a certain pattern, I can pipe it to grep, like file testfile hello there my friends command $ cat testfile | grep 'hello' hello #this will be highlightd this will…
ewok
  • 4,091
  • 15
  • 46
  • 66
84
votes
22 answers

Monitoring a file until a string is found

I am using tail -f to monitor a log file that is being actively written to. When a certain string is written to the log file, I want to quit the monitoring, and continue with the rest of my script. Currently I am using: tail -f logfile.log | grep -m…
Alex Hofsteede
  • 1,501
  • 1
  • 12
  • 9
67
votes
4 answers

Grep-like functionality for Notepad++?

I would like to have some grep-like functionality in Notepad++. In it simplest version, I would like to be able to copy all lines from the current buffer that contain the word foo to a new buffer. I can use the TextFX plugin to hide all lines…
Frank Meulenaar
  • 1,659
  • 4
  • 18
  • 23
65
votes
1 answer

What is the difference between grep, pgrep, egrep, fgrep?

I'd like a walk through on the differences between grep, pgrep, egrep, and fgrep and how I would use them.
Eric Leschinski
  • 6,828
  • 6
  • 45
  • 51
64
votes
3 answers

Can GNU Grep output a selected group?

Is it possible to use GNU grep to get a matched group from an expression? Example: echo "foo 'bar'" | grep -oE "'([^']+)'" Which would output "'bar'". But I would like to get just "bar", without having to send it through grep one more time (ie. get…
Torandi
  • 928
  • 1
  • 7
  • 9
62
votes
4 answers

Powershell equivalent of `grep -r -l` (--files-with-matches)

In Powershell, how do I list all files in a directory (recursively) that contain text that matches a given regex? The files in question contain really long lines of incomprehensible text, so I don't want to see the matching line -- just the…
Michael Kropat
  • 895
  • 2
  • 7
  • 12
60
votes
3 answers

How can I "grep" recursively filtering the name of the files I want with wildcards?

When I want to perform a recursive grep search in the current directory, I usually do: grep -ir "string" . But that command searches inside all kinds of files, including binary files (pictures, audio, video, etc...) which results in a very slow…
OMA
  • 1,620
  • 4
  • 18
  • 25
54
votes
2 answers

How can I grep with color in Mac OS X's terminal?

I recently found that on Mac OS X I can set this up in my shell ~/.profile so that when I use grep it will print the matches in color (white one red). alias grep='GREP_COLOR="1;37;41" LANG=C grep --color=auto' But setting up an alias seems like…
cwd
  • 17,668
  • 42
  • 121
  • 159
53
votes
6 answers

recursive grep: exclude specific directories

I use recursive grep a lot to find source files with specific content. grep -Rni "myfunc" . On large codebases, this can get slow, so I use --incldue to restrict/whitelist extensions. grep -Rni --include=*.java "myfunc" . However, it would be more…
gabor
  • 261
  • 2
  • 4
  • 8
1
2 3
73 74