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

1140 questions
301
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…
288
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?
226
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,609
106
votes
9 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,641
98
votes
9 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…
94
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
  • 11,561
92
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,045
88
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…
80
votes
4 answers

how to grep and print the next N lines after the hit?

I would like to grep for an occurrence in a text file, then print the following N lines after each occurrence found. Any ideas?
719016
  • 4,683
73
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,800
73
votes
5 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…
71
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…
71
votes
1 answer

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

I'd like a walkthrough on the differences between grep, pgrep, egrep, and fgrep and how I would use them.
Eric Leschinski
  • 7,393
  • 7
  • 51
  • 51
68
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
  • 968
56
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
  • 18,788
  • 43
  • 123
  • 162
1
2 3
75 76