Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

85

You can use grep anyway to search through the file - it does not really care if the input file is really text or not. From 'man grep': -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option. --binary-files=TYPE If the first few bytes of a file indicate that the file contains ...


75

I guess you could use tail to only output that last 4GB or so by using the -c switch -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file You could probably do something with dd too by setting bs=1 and skiping to the offset you want to start e.g. dd if=file bs=1024k skip=12g | grep something


67

+1 for @jamzed terse answer, however the OP might need some explanation: ps | grep "[d]jango" Using that regex you are launching a process which its ps string will not match itself, since the regexp matches "django" and not "[d]jango". That way you'll exclude the process that has the string "[d]jango" which in this case is grep; The same can be applied to ...


55

I don't know about using awk instead of grep, but this works for me: tail -f file.log | grep -Ev '(ELB|Pingdom|Health)' EDIT: As dmourati and Caleb pointed out, you could also use egrep instead of grep -E for convenience. On some systems this this will be an link to the same binary, in others a copy of it supplied by the grep package. Either way it lives ...


41

Pipe it through strings, which will strip out all of the binary code leaving just the text.


33

There's no reason that's preferable syntax on its own. It's sometimes used as a hack when "grepping" for a process (e.g., ps aux | grep [a]pache). Using that syntax prevents grep from matching its own command line in the process list. See How to prevent "ps" reporting its own process?


32

$ nc localhost 9571 | awk -F: '/rating/ { print $2 }'


32

I'm just posting this because some of the comments asked for it. What I end-up using was (15 GB file). It worked very fast and saved me a ton of time. tail -f -c 14G file | grep something I also did a very rudimentary benchmark on the same file. I tested: grep xxx file // took for-ever (> 5 minutes) dd if=file bs=1 skip=14G | grep xxx // ...


30

ps | grep [d]jango ps | grep d[j]ango ... ps | grep djang[o]


30

grep -F '-ref.alleles' is equivalent to: grep -F -ref.alleles (none of the characters between the apostrophes are shell metacharacters, so quoting them has no effect.) This is in turn equivalent to: grep -F -r -e f.alleles by normal parsing of - prefixed options. The -e option takes an argument, but -F and -r don't. Since you didn't specify any files ...


28

Use find ... -print0 | xargs -0 ... e.g. find /path/to -name "*.html" -print0 | xargs -0 grep -l "rumpus" from the find man page -print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that ‘-print’ uses). This allows file names that contain ...


25

You might have a look at zgrep. >$ zgrep -h grep through gzip files usage: zgrep [grep_options] pattern [files]


22

The zgrep program is available for Linux (and perhaps some Unix too). This will decompress the files and then grep through them.


21

Try piping it to egrep with a pipe separated lists of words you want to filter out: tail -f log_file | egrep -v 'ELB|Pingdom|Health' Note that using parenthesis around the list of matches is optional. Since the | is treated as a logical OR operator by grep whether it occurs as part of a sub-group or not. '(ELB|Pingdom|Health)' would function exactly the ...


21

Don't use cat for that. Instead use grep DATABASE * or grep -n DATABASE * (if you want to know the line numbers as well as the filenames) directly. See useless use of cat. To clarify a bit more: cat * actually concatenates all files as it feeds them to grep through the pipe, so grep has no way of knowing which content belongs to which file, and indeed can'...


19

As far as GNU grep go, this will show you the number of lines before the match: # grep -B number Equivilent for after: # grep -A number You can download GNU Grep here: http://www.gnu.org/s/grep/


19

This doesn't answer the Title question, but it will do what you are wanting to do. Use tac to reverse the file, then use grep to find your string. If your string only occurs once or a known number of times in the file, then let it run until it finds the known number of occurrences. That way, if your assumption about where it is in the file is incorrect, ...


18

Use pgrep instead: pgrep -lf django


17

This would be my approach: find dev -type f -print0 | \ # find all files xargs -0 grep 'extends MY_Output' | \ # search for your string cut -d/ -f2 | \ # extract web folder name sort | uniq # eliminate duplicates Note use of the print0 parameter to find and the -0 (zero) flag to xargs, which prevents ...


17

You have to escape the regex. ack 'console\.log\(foo' (You should escape the . so that you don't match "consoleflog", because . matches any single character) And if you would rather not do that, do this to quote every metacharacter automatically. ack -Q 'console.log(foo'


16

There is, unsurprisingly, a tool for this: grepcidr. It is not included by default with any system I'm aware of, but you can download it from here, and it is in both the Ubuntu package repository and the FreeBSD ports collection as well. (Version 2.0 works with IPv6 networks too)


15

You do not need to use xargs, because find can execute commands itself. When doing this, you do do not have to worry about the shell interpreting characters in the name. find /path/to -name "*.html" -exec grep -l "rumpus" '{}' + from the find man page -exec command {} + This variant of the -exec action runs the specified command on the selected ...


15

pv operates on pipes (not commands) -- It is a volume gauge showing how much data has gone past a given point in the pipeline. Your grep command is not a pipeline (| - the pipe operator is nowhere to be found) - it's just a single command doing its thing. pv can't help you here, you just have to trust that grep is actually doing its thing on all of the ...


15

With GNU grep (tested with version 2.6.3): git status | grep -Pzo '.*Untracked files(.*\n)*' Uses -P for perl regular expressions, -z to also match newline with \n and -o to only print what matches the pattern. The regex explained: First we match any character (.) zero or multiple times (*) until an occurence of the string Untracked files. Now, the part ...


14

Use awk as follows: $ echo '*EXTRA File 78223 C:\foo.pdf' | awk '$3 > 1048576 {print $0;}' $ echo '*EXTRA File 78223 C:\foo.pdf' | awk '$3 > 40000 {print $0;}' *EXTRA File 78223 C:\foo.pdf


14

Quanta beat me to it, but I'll include a sed variant if you're that way inclined: nc localhost 9571 | sed -ne 's/^rating://p' Ditto what MadHatter said, though. Your current solution is perfectly sound. (Although I'd grep for "^rating:" rather than just the word to ensure you only get the line you want.)


14

My answer is a variation on the typical answer for searching for "foobar" in a ps listing. The argument of "-A" "ps" is more portable than "aux", I believe, but this change is irrelevant to the answer. The typical answer looks like this: $ ps -A -ww | grep [f]oobar Instead I use this pattern: $ ps -A -ww | grep [^]]foobar The main advantage is that it's ...


13

This happened because the first thing > does is to create the file it wants to write to - and if the file already exists, its contents will be deleted. (Also, there's no need at all to use cat in your statement since grep works on files, not just on STDIN.) The correct way to do this is to use a temporary file either to read from or to write to. So ...


12

If you don't mind using sed, here is a possible solution git status | sed -n -e '/Untracked files/,$p'


11

Oh wait, this works: ps | grep django | grep -v grep


Only top voted, non community-wiki answers of a minimum length are eligible