grep

grep a file, but show several surrounding lines?

I would like to grep for a string, but show the preceding 5 lines and following 5 lines as well as the matched line. I'm scanning for errors in a logfile, and want to see the context. Any clues for the clueless? ...

Summary of differences in Regular Expression syntax for various tools and languages?

I can never remember the differences in regular expression syntax used by tools like grep and awk, or languages like Python and PHP. Generally, Perl has the most expansive syntax, but I'm often hamstrung by the limitations of even egrep ("extended" grep). Anyone know of a site that lists the differences in a concise and easy-to-read fas...

How do I find broken NMEA log sentences with grep?

My GPS logger occassionally leaves "unfinished" lines at the end of the log files. I think they're only at the end, but I want to check all lines just in case. A sample complete sentence looks like: $GPRMC,005727.000,A,3751.9418,S,14502.2569,E,0.00,339.17,210808,,,A*76 The line should start with a $ sign, and end with an * and a tw...

Regex to match unique substrings

Here's a basic regex technique that I've never managed to remember. Let's say I'm using a fairly generic regex implementation (e.g., grep or grep -E). If I were to do a list of files and match any that end in either ".sty" or ".cls", how would I do that? ...

how do you search for files containing dos line endings (CRLF) with grep under linux?

something like grep -IUr --color '\r\n' . the above seems to match for literal 'rn' which is not what is desired the output of this will be piped through xargs into todos to convert crlf to lf like this grep -IUrl --color '^M' . | xargs -ifile fromdos 'file' ...

What are good grep tool for Windows?

Any recommendations on grep tools for Windows? Ideally ones that could leverage 64-bit OS. I'm aware of cygwin, of course, and have also found PowerGREP, but I'm wondering if there are any hidden gems out there. ...

Use grep to find content in files and move them if they match

I'm using grep to generate a list of files I need to move: grep -L -r 'Subject: \[SPAM\]' . How can I pass this list to the mv command and move the files somewhere else? ...

Count number of occurences of token in a file

I have a server access log, with timestamps of each http request, I'd like to obtain a count of the number of requests at each second. Using sed, and cut -c, so far I've managed to cut the file down to just the timestamps, such as: 22-Sep-2008 20:00:21 +0000 22-Sep-2008 20:00:22 +0000 22-Sep-2008 20:00:22 +0000 22-Sep-2008 20:0...

Colored grep?

Sometimes coloring a logfile or other gives a good overview when looking for stuff and behaviors I just saw that grep have a coloring feature grep -C 99999 --color <regexp> <filename> What other methods are there? ...

How can I write a shell script to direct grep data into a date-based filename?

I basically want to do this: grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log ...with of course the filename being example.com.2008-09-27-11:21:30.log I'd then put this in crontab to run daily. ...

How to do something with bash when a text line appear to a file

I want to run a command as soon as a certain text appears in to a log file. How do I do it in bash? ...

Deleting multiline text from multiple files

I have a bunch of java files from which I want to remove the javadoc lines with the license [am changing it on my code]. The pattern I am looking for is ^\* \* ProjectName .* USA\.$ but matched across lines Is there a way sed [or a commonly used editor in Windows/Linux] can do a search/replace for a multiline pattern? ...

What's the best way to find a string/regex match in files recursively? (UNIX)

I have had to do this several times, usually when trying to find in what files a variable or a function is used. I remember using xargs with grep in the past to do this, but I am wondering if there are any easier ways. ...

grep: matching on literal "+"

I need to find occurrences of "(+)" in my sql scripts, (i.e., Oracle outer join expressions). Realizing that "+", "(", and ")" are all special regex characters, I tried: grep "\(\+\)" * Now this does return occurrences of "(+)", but other lines as well. (Seemingly anything with open and close parens on the same line.) Recalling that ...

Quick unix command to display specific lines in the middle of a file?

Trying to debug an issue with a server and my only log file is a 20GB log file (with no timestamps even! Why do people use System.out.println() as logging? In production?!) Using grep, I've found an area of the file that I'd like to take a look at, line 347340107. Other than doing something like head -<$LINENUM + 10> filename | tail -...

Fast recursive grepping of svn working copy

I need to search all cpp/h files in svn working copy for "foo", excluding svn's special folders completely. What is the exact command for GNU grep? ...

grep --exclude/--include syntax (do not grep through certain files)

I'm looking for the string "foo=" (without quotes) in text files in a directory tree. It's on a common Linux machine, I have bash shell: grep -ircl "foo=" * In the directories are also many binary files which match "foo=". As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPE...

Make a Perl-style regex interpreter behave like a basic or extended regex interpreter

I am writing a tool to help students learn regular expressions. I will probably be writing it in Java. The idea is this: the student types in a regular expression and the tool shows which parts of a text will get matched by the regex. Simple enough. But I want to support several different regex "flavors" such as: Basic regular expres...

Shell script to find bigrams.

I'm making a shell script to find bigrams, which works, sort of. #tokenise words tr -sc 'a-zA-z0-9.' '\012' < $1 > out1 #create 2nd list offset by 1 word tail -n+2 out1 > out2 #paste list together paste out1 out2 #clean up rm out1 out2 The only problem is that it pairs words from the end and start of the previous sentence. eg for th...

Regex in awk and WinGrep

So I'm looking for a pattern like this: size='0x0' in a log file - but I'm only interested in large sizes (4 digits or more). The following regex works great in EditPadPro (nice tool BTW) size='0x[0-9a-fA-F]{4,} But the same regex does not work in awk - seems like the repetition {4,} is messing it up. Same with WinGrep - any idea fr...