Grep is great for printing out a line (or multiple lines) that match a given value. However, I have found it sometimes helpful to search large files, especially log files, at get the line I want plus a few before and after.
If I want to find errors in the /var/log/messages file and I know the line will contain the word "ERROR", I can use the below to get all the lines matching (case-sensitive in this example) as well as 3 before and 3 after.
grep ERROR -B 3 -A 3 /var/log/messages
Showing posts with label grep oneliner. Show all posts
Showing posts with label grep oneliner. Show all posts
Sunday, July 17, 2011
Sunday, July 3, 2011
grep oneliner: search for a value recursively
Ever forget exactly which file you had placed some particular code in and really wanted to find it quick. Here's a grep oneliner to do just that. In this example, I am looking for "#define MAX_VALUE" in a directory containing many source files and sub-directories.
grep -R --include "*.c" "#define MAX_VALUE" .
Note: the "." indicates the current directory in Linux. If your files are in a different tree, just replace the "." with that tree's root location. For example, if you wanted to look /usr/bin/local/:
grep -R --include "*.c" "#define MAX_VALUE" /usr/bin/local
grep -R --include "*.c" "#define MAX_VALUE" .
Note: the "." indicates the current directory in Linux. If your files are in a different tree, just replace the "." with that tree's root location. For example, if you wanted to look /usr/bin/local/:
grep -R --include "*.c" "#define MAX_VALUE" /usr/bin/local
Subscribe to:
Posts (Atom)