Questions tagged [grep]

grep is a command-line tool for searching text patterns in files.

The grep utility searches a pattern (regular expression) in a text file. There are many options to control how matches are performed and how to display matches.

External references

Tutorials

Further reading

See also grep on Unix & Linux Stack Exchange for more detailed reading.

322 questions
78
votes
5 answers

Getting the last match in a file using grep

What's the best way of getting only the final match of a regular expression in a file using grep? Also, is it possible to begin grepping from the end of the file instead of the beginning and stop when it finds the first match?
Acorn
  • 947
  • 1
  • 6
  • 10
76
votes
5 answers

How do I grep through binary files that look like text?

I have binary files that should be text (they're exported logs), but I can't open it with less (it looks ugly - it looks like a binary file). I found that I could open it with vi and I can cat it (you'll see the actual logs), but what I'd really…
Robyn Smith
  • 861
  • 1
  • 7
  • 5
73
votes
12 answers

Do you have any useful awk and grep scripts for parsing apache logs?

I can use log analyzers, but often I need to parse recent web logs to see what's happening at the moment. I sometimes do things like to figure out top 10 ips that request a certain file cat foo.log | grep request_to_file_foo | awk '{print $1}' | …
deadprogrammer
  • 1,661
  • 7
  • 24
  • 25
58
votes
7 answers

How to prevent "ps" reporting its own process?

$ ps | grep django 28006 ttys004 0:01.12 /usr/bin/python bin/django celeryd --beat 51393 ttys005 0:01.45 /usr/bin/python bin/django celeryd -l INFO 51472 ttys005 0:01.29 /usr/bin/python bin/django celeryd -l INFO 51510 ttys005 0:01.89…
Steve Bennett
  • 5,539
  • 12
  • 45
  • 57
50
votes
5 answers

Linux Command to find Strings in Binary or non ascii file

Is there any linux command to extracts all the ascii strings from an executable or other binary file? I suppose I could do it with a grep, but I remember hearing somewhere that such a command existed?
Ethan Heilman
  • 711
  • 2
  • 8
  • 10
47
votes
7 answers

Recursive text search with grep and file patterns

Given this example folder structure: /folder1/file1.txt /folder1/file2.djd /folder2/file3.txt /folder2/file2.fha How do I do a recursive text search on all *.txt files with grep from "/"? ("grep -r *.txt" fails when run from "/", since…
Anders Sandvig
  • 649
  • 2
  • 9
  • 10
40
votes
9 answers

How to get pgrep to display full process info

Is there any way to get pgrep to give me all the info about each process that ps does? I know I can pipe ps through grep but that's a lot of typing and it also gives me the grep process itself which I don't want.
JoelFan
  • 2,165
  • 5
  • 24
  • 30
39
votes
4 answers

In tail -f, how do I filter out stuff that has certain keywords?

I want to tail -f my logs. However, I want to filter out everything that has the words: "ELB", "Pingdom", "Health"
Alex
  • 8,111
  • 24
  • 71
  • 99
35
votes
3 answers

Grep in a huge log file (>14 GB) only the last x GB?

I need to search something in a huge log-file (over 14 GB). I'm pretty sure it's in the last 4 GB or so. Is there a way to skip the first X GB to speed things up?
Roger
  • 790
  • 1
  • 6
  • 17
33
votes
3 answers

How to view hidden files using Linux `find` command

On a Linux server, I need to find all files with a certain file extension in the current directory and all sub-directories. Previously, I have always used the following command: find . -type f | grep -i *.php However, it doesn't find hidden files,…
Tom
  • 4,157
  • 11
  • 41
  • 52
32
votes
3 answers

How can I handle spaces in file names when using xargs on find results?

One of my common practices is to perform greps on all files of a certain type, e.g., find all the HTML files that have the word "rumpus" in them. To do it, I use find /path/to -name "*.html" | xargs grep -l "rumpus" Occasionally, find will return a…
abeger
  • 461
  • 1
  • 4
  • 6
32
votes
8 answers

how to find out mac addresses of all machines on network

Is there some easy way to find out mac address of all machines on my network rather than doing an SSH into each and ifconfig | grep HWaddr if there are 300 machines on network I really need some easy solution.
Registered User
  • 1,453
  • 5
  • 18
  • 37
30
votes
7 answers

Don't need the whole line, just the match from regular expression

I simply need to get the match from a regular expression: $ cat myfile.txt | SOMETHING_HERE "/(\w).+/" The output has to be only what was matched, inside the parenthesis. Don't think I can use grep because it matches the whole line. Please let me…
Alex L
  • 581
  • 2
  • 5
  • 11
28
votes
2 answers

When to use single quotes, double quotes, or no quotes in grep?

While trying to search for a simple pattern "hello" in a file, all the following forms of grep work: grep hello file1 grep 'hello' file1 grep "hello" file1 Is there a specific case where one of the above forms work but others do not. Does it make…
TheLameProgrammer
19
votes
1 answer

Why bracket a single letter in a grep regex?

I've seen several instances where people are doing this: grep [f]oobar But I don't understand why that is preferable to grep foobar
hortitude
  • 565
  • 1
  • 3
  • 10
1
2 3
21 22