Skip to main content

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
294 votes
5 answers
89k 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
286 votes
14 answers
700k 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
225 votes
4 answers
57k views

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 ...
Tommy B.'s user avatar
  • 2,599
94 votes
8 answers
167k views

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 ...
Mahadevan Sreenivasan's user avatar
92 votes
9 answers
120k views

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 ...
Nicolas Raoul's user avatar
91 votes
3 answers
280k views

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 ...
Oskar Szura's user avatar
  • 1,035
89 votes
8 answers
96k views

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 '...
ewok's user avatar
  • 4,291
87 votes
22 answers
151k 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
72 votes
4 answers
78k views

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 ...
Frank Meulenaar's user avatar
68 votes
3 answers
104k 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,730
68 votes
4 answers
62k 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
67 votes
3 answers
96k views

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 ...
Torandi's user avatar
  • 958
67 votes
1 answer
113k views

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's user avatar
65 votes
4 answers
189k 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,427
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
53 votes
5 answers
114k 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.7k
53 votes
6 answers
67k views

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 --...
52 votes
3 answers
62k views

find / grep command without searching mounted shares

When I used the find command, I almost always need to search the local drives. But, I almost always have super large network shares mounted and these are included in the search. Is there an easy way ...
Flotsam N. Jetsam's user avatar
41 votes
5 answers
73k 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
36 votes
13 answers
153k views

What regular expression can I use to match an IP address?

With the following grep syntax I want to match all IP address in a file (from a ksh script) grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' file The problem: It also matches words (...
jennifer's user avatar
  • 1,127
33 votes
1 answer
58k views

Combine tail -f with grep?

Hi I wanna keep looking at a log file, but I also don't wanna see irrelevant stuff, I'm only interested in anything with "foobar" in it. So if I was tailing the file I would do tail file | grep "...
user893730's user avatar
  • 1,227
32 votes
3 answers
40k views

Counting total number of matches with grep instead of just how many lines match

Does grep offer a way to count the total number of matches it makes? The -c option only returns the number of lines that matched the regex, but in this case I have multiple matches per line.
William's user avatar
  • 557
31 votes
18 answers
85k views

List only the device names of all available network interfaces

I want to get a list of all available Network-Device Names on my Linux server. I figured that ifconfig would do the job, however ifconfig produces quite much output: eth0 Link encap:Ethernet ...
ftiaronsem's user avatar
30 votes
7 answers
36k 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
29 votes
3 answers
29k 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
26 votes
3 answers
28k 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
25 votes
4 answers
12k views

Command-line CSS selector tool [closed]

Question What tool (preferably for Linux) can select the content of an HTML element based on its CSS path? Example For example, consider the following HTML document: <html> <body> &...
Dave Jarvis's user avatar
  • 3,258
24 votes
3 answers
9k views

In Unix "less", can I jump to the next line that does NOT contain X?

In the Unix less tool, is it possible to jump to the next line that does not contain a certain expression? I sometimes have to look through files that contain thousands of similar adjacent lines ...
dehmann's user avatar
  • 2,323
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,490
23 votes
3 answers
227k 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
23 votes
3 answers
87k views

How to find file with name="php.ini" on linux using grep command

How to find file with name="php.ini" on linux using grep command ? Can anybody show me ?
Misko's user avatar
  • 233
23 votes
6 answers
60k views

Recursive zgrep not working

I have a directory hierarchy that contains numerous .gz files. I want to be able to recursively grep them for the string "foo". From what I've read online the following should work: zgrep -R -H "...
Nosrettap's user avatar
  • 1,011
22 votes
7 answers
72k views

Advantages of cat'ing file and piping to grep

Are there any additional advantages of cat'ing a file and piping it to grep, besides convenience? The convenience being that, when I retrieve commands such as those below from my history, the cursor ...
Dexygen's user avatar
  • 325
22 votes
3 answers
3k views

How to prevent a password or other sensitive information from being stored in the bash history when using grep?

I had to grep recently for a password that may have been saved in a file. I did not want that line to show up in the bash history. How can I prevent a password or other sensitive information from ...
Leo's user avatar
  • 575
22 votes
3 answers
39k 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,883
22 votes
1 answer
45k views

does grep regex work differently on mac?

Just trying to figure out basic use of regexes with grep (or egrep) in mac terminal (BSD grep - 2.5.1-FreeBSD). File to examine (pow.txt) contains the lines : kiytytytyty and blob.mkv command ...
Rmy5's user avatar
  • 323
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
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
2 answers
7k views

ack-grep show the matched files name only. Do not show the matched content

I'm searching some keywords among minified files. It doesn't make sense to show those minified content. How could I use ack-grep 'SEARCH_WORD' and show the matched file names only
poc's user avatar
  • 684
20 votes
5 answers
112k views

regex : does not begin by "pattern"

I have a lot of lines in my LIST file and want to list only lines whose name does not start by (or contains) "git". So far I have : cat LIST | grep ^[^g] but I would like something like : #not ...
Vulpo's user avatar
  • 315
20 votes
2 answers
18k views

Edit first line of large text file

I have a huge text file, far too big for the whole thing to be paged into memory. All I need to do with this text file is edit the first line (its a CSV file and I need to alter the titles). Is there ...
lynks's user avatar
  • 1,871
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
73k 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
18 votes
4 answers
11k views

How to use ack to search for file name

I have started using ack which is much faster than grep. However using ack I want to search for file name rather than file contents. Is there a way to do that?
Nadal's user avatar
  • 959
18 votes
3 answers
21k views

Finding and deleting lines from all files recursively

I have managed to grep the occurences of a keyword by using grep "KeyWord" . -r -n -i -I but, the occurences of the KeyWord are too large. I just want to delete all the lines containing that word. ...
SurenNihalani's user avatar
18 votes
3 answers
16k views

Grep default color option

grep is one of the most commonly used command in linux. It feels to me that its basic feature to highlight the string you searched for on the output lines. this can be achieved by --color option. ...
Srujan Kumar Gulla's user avatar
18 votes
3 answers
26k views

How to have grep print only the matching text plus X surrounding characters

I need something similar to grep -A and grep -B but for characters. In other words, I have a file with incredibly long lines, e.g.: [thousands of characters] mytext [thousands of characters] If I do ...
CaptSaltyJack's user avatar
17 votes
3 answers
2k views

Manually select/unselect lines before forwarding to stdout

Is there any way to grep interactively, meaning that I have a bunch of lines coming from somewhere that I want to pipe to further processing, with the option to manually specify the lines I want? ...
Bowi's user avatar
  • 1,527
17 votes
7 answers
72k views

How to list all source files (*.c, *.cpp, *.h)

I'd like to find source files (*.c, *.cpp, *.h) that contain in Linux/MinGW/Cygwin, and recursively in all sub directories. My basic idea is using find and grep. However, building a regular ...
Nullptr's user avatar
  • 341
17 votes
5 answers
34k views

grep changing the delimiter

any way we can change the delimiter that uses grep? I think, by default, grep uses \n as the delimiter.
user avatar

1
2 3 4 5
23