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

learn more… | top users | synonyms

41
votes
12answers
49k views

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 ...
33
votes
5answers
5k views

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 ...
23
votes
8answers
46k views

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 ...
23
votes
4answers
33k views

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 ...
23
votes
5answers
24k views

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 ...
22
votes
4answers
5k views

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"
18
votes
1answer
455 views

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
16
votes
7answers
2k views

Can awk be used instead?

I would like to get the number from rating as output from this # nc localhost 9571 language: language:en_ZA.UTF-8 language:en_ZW.UTF-8 session-with-name:Ubuntu Classic (No effects):gnome-session ...
12
votes
3answers
825 views

grep behaving differently on Fedora vs Ubuntu

So when I run this in Fedora I'm seeing this: $ ls hmm_data/indivA12_AATAAG/refs/par1/ 2R-orths.alleles 2R-ref.alleles $ ls hmm_data/indivA12_AATAAG/refs/par1/ | grep -F '-ref.alleles' ...
12
votes
5answers
2k views

Grepping through .gz log files

Does there exist a magical shell piping which would allow easily to grep through bunch of .gz log files without needing to extract them somewhere? .gz files are Apache logs, result of log rotation. ...
11
votes
2answers
9k views

When to use single-quote, double-quote 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 ...
10
votes
4answers
11k views

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? ...
8
votes
2answers
10k views

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 ...
8
votes
6answers
5k views

How do i get the default gateway in LINUX given the destination?

I'm trying to get the default gateway, using the destination 0.0.0.0 i used this command: netstat -rn | grep 0.0.0.0 and it returns this list: Destination - Gateway - Genmask - Flags - ...
8
votes
4answers
2k views

Searching for literal “> \” using ack-grep

I am looking for lines that literally have a greater than character (a ">") followed by a space followed by a backslash character (a "\") i.e., a line with this: > \ I thought escaping would allow ...
8
votes
4answers
6k views

Fast extraction of a time range from syslog logfile?

I've got a logfile in the standard syslog format. It looks like this, except with hundreds of lines per second: Jan 11 07:48:46 blahblahblah... Jan 11 07:49:00 blahblahblah... Jan 11 07:50:13 ...
7
votes
5answers
424 views

Grep-ing gzipped files [duplicate]

I have a set of 100 log files, compressed using gzip. I need to find all lines matching a given expression. I'd use grep, but of course, that's a bit of a nightmare because I'll have to unzip all ...
7
votes
9answers
4k views

How to grep the IP address from ifconfig output

Following is my ifconfig output eth0 Link encap:Ethernet UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX ...
7
votes
3answers
3k views

echo based on grep result

I need a one liner which displays 'yes' or 'no' whether grep finds any results. I have played with grep -c, but without success.
6
votes
6answers
6k views

How to Find a file in FreeBSD

I'm trying to locate all copies of example.filename on my FreeBSD server. What's the best / easiest / most efficient way to do this?
6
votes
3answers
233 views

OS/2 grep had a great feature where it would show you n lines BEFORE the search item was found. Is there an equivalent in unix anywhere?

Solaris would be preferable, but I'll take linux or something I can compile. Does such a beast exist?
6
votes
2answers
5k views

cat * | grep something… what file is the result in?

If one was to run the following command cat * | grep DATABASE the shell would spit out all the lines in * files that contained the word DATABASE in them. Is there any way to also spit out what file ...
6
votes
4answers
764 views

Kill process by command name

I would like to kill a java process based on the command name... Whats the best way to do it? (i.e. when you do ps -f the name in the CMD column).
6
votes
2answers
1k views

Grepping for CIDR ranges

From time to time I want to grep CIDR ranges out of my Apache log files. This is easy for ranges that fall on the natural boundaries (/8, /16 and /24) but not so easy for other ranges such as /17 and ...
6
votes
2answers
835 views

Can I run grep against a zOS dataset via Unix system services?

I have a search I'd like to run against a zOS dataset, and I know how to form the grep command. I have access to the machine via Unix system services, how to I address a dataset from that ...
6
votes
1answer
1k views

Ack search for literal strings [closed]

I am sick of having to escape things when I want to search for part of an html tag. How can I ack search for exactly what I type without having to escape stuff? e.g. ack-grep 'console.log(foo' I ...
5
votes
2answers
4k views

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 ...
5
votes
1answer
244 views

screen causes “tail -F | grep” to echo unmatched lines

This is Ubuntu (3.13.0-29-generic #53), trying to do a simple bash script to monitor a log file for matching lines. If I do the following, there is no output in the bash shell except for matches, ...
5
votes
4answers
4k views

Grep progress bar using pv (pipe viewer)

I've got a huge directory on my computer and I need to search in every ruby file inside for a string. I could have done it like this : grep -R "string" *.rb but it takes really long and I'd like to ...
5
votes
6answers
2k views

How to remove a tagged block of text in a file?

How can I remove all instances of tagged blocks of text in a file with sed, grep, or another program? If I have a file which contains: random text // START TEXT internal text // ...
5
votes
2answers
766 views

List top 404 URLs in Apache access_log

I'm looking for a command to list the 10 or 20 top 404 error URLs out of my apache access_log. Can anyone help me with this? thanks
4
votes
4answers
2k views

Combine multiple unix commands into one output

I need to search our mail logs for a specific e-mail address. We keep a current file named maillog as well as a week's worth of .bz2 files in the same folder. Currently, I'm running the following ...
4
votes
3answers
3k views

grep or sed to find a integer value above a given amount?

This is in reference to another question: How do I use robocopy to list all files over a certain size recursively? I would like to parse output of a command (or cat a log file) and find a string ...
4
votes
1answer
10k views

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 using the following command: find . -type f | ...
4
votes
3answers
2k views

How can i send /var/mail/root again?

Through some missconfiguration i've a lot of emails in /var/mail/root and /var/mail/www-data How can i "loop" over these files and send each mail another time? Thanks in advance!
4
votes
3answers
368 views

Is there a way to grep with a constrained file type?

right now i am using something like this: find . -name "*.xml" | xargs grep -l "foobar" it works, but i was wondering if grep has this functionality built in?
4
votes
4answers
158 views

Possible to use grep simply for finding files in a directory structure?

I like using grep but sometimes I just want to search for the names of files in a directory tree, but don't want grep to open the files and search inside them. Can grep do this?
4
votes
3answers
5k views

iptables - Clear all PREROUTING rules with a specific destination address

I have a script that adds iptable PREROUTING rules. They all have the same to address. When I run this: iptables --list PREROUTING -t nat I see output like this: DNAT tcp -- anywhere ...
3
votes
4answers
1k views

“watching” a log on FreeBSD vs Linux

On Linux systems I can watch -n1 tail /var/log/whatever.log or watch -n1 grep somestuff /var/log/whatever.log To show updates to a log every 1 seconds. On FreeBSD however, the watch command does ...
3
votes
9answers
3k views

grep to display folders which contain files which contain a string

I need to run a grep on my server to search for files which extend a CodeIgniter 1 file, as we are upgrading to CI2. In one folder contains several hundred site-specific folders like such: ...
3
votes
2answers
2k views

Is there a way to find a specific word in all files/folders in Linux?

I usually use grep word * while being in a folder which includes files that have that word. But in this folder i also have folders which under them there are files that have that word, what is the ...
3
votes
2answers
1k views

Cron job checking for changes in Git repository

We have just moved our server configs to a Git repository. Therefore there should not be any changes in any of the repository folders. I was thinking about how I could set up a cron job to check for ...
3
votes
1answer
267 views

grep recursive exclusive search

I have text I want to recursively search in mydir/. I would normally type grep -r "text to find" mydir/" but what would I type if I wanted to search all the files except a specific one? For example, ...
3
votes
2answers
564 views

egrep matching group not printing?

I'm trying to create a bash script that will grep lines from a file using egrep. I've created the regex that should group the information I want, the issue is trying to get the output. I've been ...
3
votes
2answers
2k views

How can I use grep to find the most frequently recurring errors in my log file?

I've got a PHP application that is generating large amounts of warnings and notices in the log file.. I'm fixing these one at a time. I would like to be able to fix the most frequently occurring ...
3
votes
1answer
32 views

How to find file which have not specific content?

I have a lot of files which already have SPF records defined grep -i v=spf *.db /var/named/domain.com.db domain.com. 14400 IN TXT "v=spf1 +a +mx +ip4:XXX.XXX.XXX.XXX ?all" And want ...
3
votes
2answers
669 views

Local full text index for a directory tree, a quicker alternative for grep -R

I have a large directory tree of source files. I want to search around all these files. My first instinct is to use grep -R or ack, but either way they just sequentially read all the files. Is there ...
3
votes
1answer
142 views

Need help grepping postfix log

I'm trying to search for an unknown string (Message ID) in a log based on a known string (recipient address), and then grep the unknown string to output the entire relevant log information. I'm able ...
3
votes
2answers
243 views

Extracting a line section of mysql backup using sed

I occasionally need to extract a single record from a mysqlbackup To do this, I first extract the single table I want from the backup... sed -n -e '/CREATE TABLE.*usertext/,/CREATE TABLE/p' ...
2
votes
9answers
1k views

How to reproduce grep's --recursive option in an old version of grep

I am working on an ancient UNIX whose grep lacks the -r/--recursive option. I need to find an error that our application is causing, but I do not know which log file our application is writing ...