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.

Filter by
Sorted by
Tagged with
288 votes
14 answers
698k views

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's user avatar
  • 4,010
87 votes
22 answers
150k views

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. ...
Alex Hofsteede's user avatar
30 votes
7 answers
35k views

Excluding grep from process list

I have cobbled together a command to return the process ID of a running daemon: ps aux | grep daemon_name | awk "{ print \$2 }" It works perfectly and returns the PID, but it also returns a second ...
tak's user avatar
  • 303
22 votes
3 answers
38k views

How to Combine find and grep for a complex search? ( GNU/linux, find, grep )

I'm trying to do a text search in some files that share a similar directory structure, but are not in the same directory tree, in GNU/Linux. I have a web server with many sites that share the same ...
Petruza's user avatar
  • 3,873
7 votes
2 answers
3k views

Why isn't `tail -f … | grep -q …` quitting when it finds a match?

I'm doing a pretty standard tail + grep: tail -f some_log_file.txt | grep -q known-string If I run the command without the -q: tail -f some_log_file.txt | grep known-string I see output: [Tue Feb ...
machineghost's user avatar
  • 1,102
41 votes
5 answers
72k views

Writing "tail -f" output to another file

As a continuation from my last post where I have used grep & tail -f to find occurences of "rare" events. I would like to record this in another file. I've tried turning tail -f log.txt | ...
Mike's user avatar
  • 635
3 votes
1 answer
2k views

How do I grep help output in linux?

When I use a switch that doesn't exist in commands such as lsof and ps there is a lengthy help menu that is returned. Instead of reading the whole thing I'd like to be able to grep it for the switch I'...
flashnode's user avatar
  • 419
1 vote
1 answer
3k views

Zsh grep with glob * is not working

The same command was working fine with bash git show --pretty="" --name-only HEAD | grep -i .*py zsh: no matches found: .*py Bash git show --pretty="" --name-only HEAD |grep -i ....
Knight71's user avatar
  • 113
-2 votes
2 answers
3k views

Getting this simple regular expression to match in grep

I want to match a quote, 2, a space, and any character that is not a literal dot. This is using GnuWin32 grep. Not Cygwin's grep. C:\>echo "2 008abc.html" | grep -oiP \"2 [^.] grep: [^.]': No ...
barlop's user avatar
  • 23.9k
292 votes
5 answers
88k views

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 | ...
Jeremy Powell's user avatar
53 votes
5 answers
112k views

grep for "term" and exclude "another term"

I am trying to build a grep search that searches for a term but exludes lines which have a second term. I wanted to use multiple -e "pattern" options but that has not worked. Here is an example of a ...
nelaaro's user avatar
  • 13.6k
26 votes
3 answers
27k views

Can grep show context, but not a full line?

I have a file with several very long lines. I want to grep for a string which may occur several times in the file, including possibly more than once on the one line. $ cat 2014-11-03.json | grep 218 ...
TRiG's user avatar
  • 1,340
13 votes
1 answer
27k views

Regex for "or" of multiple words in grep

[Computer]$ grep "foo|bar" filename I understand the above command should return each line in filename where there exits "foo" or "bar". The man pages confirms | as the Regex or symbol and the code ...
Ocasta Eshu's user avatar
12 votes
5 answers
9k views

Command line tool to search phrases in large number of pdf files [closed]

I'm using Opensuse 10.3 and like to know command line tools to search phrases in large number of pdf files inside a directory. In Windows XP the Explorer search allows this but is too slow. Is there ...
iceman's user avatar
  • 2,452
12 votes
2 answers
20k views

tar - exclude certain files

I wish to tar all files in a directory and its subdirectories that do NOT end in .jpg, .bmp, .gif, or png. So, given the following folders and files: foo/file.txt foo/file.gif foo/bar/file foo/bar/...
Alan's user avatar
  • 123
12 votes
3 answers
28k views

Mac OS X search and replace tool with regular expression support

I'm looking for a regular expression based search/replace GUI tool that is able to handle multiple files/subdirectories, something like grepWin but for Mac OS X. PS. I'm aware of grep and I don't ...
user187291's user avatar
10 votes
4 answers
5k views

grep does not recurse for files with a specific extension

Whenever I use grep with gnuwin32's recurse option -r and include a glob pattern for files to search (e.g. *.c), no files in the subdirectories are searched. I am using the latest grep from gnuwin32. ...
Cody Piersall's user avatar
8 votes
5 answers
7k views

How can I grep in source files for some text?

At the moment I'm using two commands, I'm sure there must be a better way... wim@wim-acer:~/ffmpeg$ find . -name "*.h" -print0 | xargs -0 grep -i invalid\ preset wim@wim-acer:~/ffmpeg$ find . -name "*...
wim's user avatar
  • 3,167
7 votes
1 answer
50k views

Grep command, to remove lines containing a specified word

I want to use grep in the following way: grep -v "END","EXPDTA" 1bmz_model1.pdb > 1bmz_model.pdb I want the grep command to remove the lines which contain the words "END" and "EXPDTA", but all i ...
Harpal's user avatar
  • 205
4 votes
2 answers
5k views

Checking if any data exists on a presumably empty storage device

So, say you've completed a full pass of: dd if=/dev/zero of=/dev/sdX bs=1M Then, you'd like to make sure the destination has been really zeroed out (ignoring the confirmation messages from dd and ...
XXL's user avatar
  • 1,469
3 votes
1 answer
4k views

Is there a limit for a line length for grep command to process correctly?

While I'm checking the results of my biostar implementation for searching primes in a fasta file, I've seen a strange result. I've originally a 70 column file and converted it into a file that has ...
kelalaka's user avatar
  • 447
2 votes
2 answers
1k views

Matching a sentence with grep

I'm trying to grep for the full sentence containing a search term. I've tried grep (^.|\.\s).*searchterm.*(\.\s|\n) but it's not working and I'm not sure why. To clarify: I want stdout to print the ...
user192380123's user avatar
2 votes
3 answers
425 views

grep -c gives inconsistent outputs

I am trying to determine the number of processes that are running via the following command line: ps aux | grep -c 'Xvfb :1' When the process "fiji :1" is not running I sometimes get a value of 0 ...
sebastian's user avatar
-1 votes
1 answer
1k views

Linux "rpl" command doesn't replace text

I'm using rpl program in linux to replace date( with pdate( in some files. But it says A Total of 0 matches replaced in 1 file searched. while grep output for date( is: ariyan@ariyan-laptop:/var/...
Ariyan's user avatar
  • 625
67 votes
4 answers
60k views

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 ...
Michael Kropat's user avatar
65 votes
3 answers
101k views

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, ...
OMA's user avatar
  • 1,690
59 votes
4 answers
175k views

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's user avatar
  • 4,347
55 votes
2 answers
33k views

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 --...
cwd's user avatar
  • 18.2k
29 votes
3 answers
28k views

How do I pipe output to date -d "value"?

I have a date like 2014-01-30 05:04:27 GMT, and if I run date -d "2014-01-30 05:04:27 GMT", the output is in my server's timezone (Thu Jan 30 16:04:27 EST 2014). With the use of grep and cut,...
LeigerGaming's user avatar
24 votes
5 answers
22k views

Grep tool for XML [closed]

I am looking for a good tool to perform grep-like operations on XML - for example, extract certain attributes only. Grep itself can't handle it - any DFA-equivalent tool can handle only non recursive ...
Adam Matan's user avatar
  • 8,390
23 votes
3 answers
225k views

How to grep a log file within a specific time period

I have a log file, each line in the log is prepend with a date, like so: 2012-03-06 11:34:48,657 blah blah blah... How do I grep this file and get only the lines from 8am to 11pm only? My ...
Rosdi's user avatar
  • 906
21 votes
5 answers
48k views

How to combine wget and grep

I have a html-page url and I want to grep it. How can I do it by wget someArgs | grep keyword? My first idea was wget -q -O - url | grep keyword, but wget's output bypass grep and arise on the ...
Nelson Tatius's user avatar
21 votes
5 answers
37k views

Ubuntu grep, find etc: "Permission denied" and "No such file or directory" output

When I use grep or find, I always get annoyed by the "Permission denied" and "No such file or directory" notices, something like this: johndoe@johndoe-desktop:/$ grep -rnP 'YII_CORE_PATH' ./ | grep -...
Vadim Samokhin's user avatar
19 votes
1 answer
41k views

Ubuntu Linux: find files between specific times?

I found an SO called using Find/Grep to search files between specific time of day Based on that and a Unix SE called Grep command to find files containing text string and move them I ended up with: ...
digitaltoast's user avatar
18 votes
11 answers
71k views

How do I search for a string in a PHP file using `grep`?

I am searching for a class declaration on a site with hundreds of PHP files. How can I do this in the current folder and subfolders using grep? I tested cding to the folder and then something like ...
user avatar
16 votes
7 answers
6k views

Unicode grep for Windows [closed]

Is there a Unicode-aware grep for Windows 32-bit?
Jeroen Wiert Pluimers's user avatar
13 votes
9 answers
21k views

grep all .java files in a directory for a particular string

How would I search all .java files for a simple string (not a regex) in the current directory and all sub-directories on Mac OS X? I just want to print a list of file and directory names that match.
John Topley's user avatar
  • 1,758
12 votes
7 answers
9k views

Using an index to make grep faster?

I find myself grepping the same codebase over and over. While it works great, each command takes about 10 seconds, so I am thinking about ways to make it faster. So can grep use some sort of index? I ...
Peltier's user avatar
  • 6,304
10 votes
3 answers
9k views

How do I grep the first 50 lines of each file in a directory recursively?

I need to search the first 50 lines of every file in a directory and its subdirectories. This will do the recursive part, but how do I limit to just the first 50 lines of each file? grep -r "...
zevlag's user avatar
  • 307
9 votes
2 answers
4k views

Grep in Mac OS X's terminal -- only displaying one color

I am used to grep being able to color the filename, the line number, and the match itself. These three ought to be different colors. This works flawlessly on a Linux terminal and even MinGW on Windows,...
Steven Lu's user avatar
  • 3,678
8 votes
3 answers
11k views

I'm having trouble with a text file being marked as a binary

I have an executable that generates a text file as its output. The problem is that the text file comes out with a binary file flag of some sort. The result is something like this: $ grep "grep ...
brightwellcd's user avatar
5 votes
2 answers
26k views

How to search a text file for strings between two tokens in Ubuntu terminal and save the output?

How can I search a text file for this pattern in Ubuntu terminal and save the output as a text file? I'm looking for everything between the string "abc" and the string "cde" in a long list of data. ...
Blue's user avatar
  • 59
5 votes
1 answer
642 views

grep simply fails when used on a few files

I've been trying for about the past 30 minutes to get this to work properly. grep is not exactly the most difficult thing to use, so I'm somewhat baffled as to why this won't work. The files I'm ...
Reid's user avatar
  • 255
5 votes
2 answers
1k views

grep - match anything but given filetype

I want to clean out my music library so I don't get any more "search for suitable plugin" messages from Rhythmbox when it sutubles across some WMA-Relic. I have the tools, but now I want to FIND ...
user avatar
4 votes
1 answer
414 views

Get additional data around match from grep

I'm wondering how to improve my grep use when being confronted with files with very long lines. Imagine something like "packed" JavaScript files that only have 1 line. Sometimes files like that spit ...
Oliver Salzburg's user avatar
4 votes
1 answer
8k views

grep says 'invalid character range'

So I'm using a combination of find and grep to filter out a list of file names from a plain text document. Here's the command I run: find /Volumes/Documents\ -\ Part\ 1/July 2009 -type f | grep -vf ...
leetbacoon's user avatar
3 votes
3 answers
3k views

Recovering text files in terminal using grep on Mac OS X Snow Leopard

I foolishly removed some source code from my Mac OS X Snow Leopard machine with rm -rf when doing something with buildout. I want to try and recover these files again. I haven't touched the system ...
littlejim84's user avatar
3 votes
1 answer
6k views

Why does grep return no results (list all files in directory)

I'm trying to count all files in a directory that match a pattern, recursively, using ls: ls -R | grep *.{cpp,h} | wc However, I get: zsh: no matches found: *.cpp ls -R does return results, ...
simont's user avatar
  • 1,382
2 votes
3 answers
26k views

Bash: Find and replace text strings

I figured this would be easy, but I'm overlooking something simple: I have a text file called test. It contains, for example, the string Alfred Hitchcock. I want to replace this with Alfred\ ...
Conor Taylor's user avatar
2 votes
2 answers
6k views

Use diff to find difference in the output of two grep command

Is it possible to diff the output of two grep commands? I am currently searching for files, using different exclude patterns, and the output it pretty long, so i wanted to see if my new pattern ...
I am not Fat's user avatar