Ah, learned the difference between -i and -I. I also like --include and --exclude, especially since it allows regex for which files to look at
If you need to include/exclude multiple things, you can go `grep pat --exclude={foo,bar,baz}` which expands to: `grep pat --exclude=foo --exclude=bar --exclude=baz`. Much easier than typing out the flag multiple times
Grep flags – the good stuff
31–40 of 53 posts
Re: Grep flags – the good stuff
#32An amazing grep trick that I use all the time: The -e flag can be used to search for multiple terms. A blank -e will search for null. Thus: Lets assume we have a log file with a bunch of relevant stuff, I want to highlight my search term, BUT I also want to keep all the other lines around for context: $ dmesg ...SNIP... [2334597.539661] sd 1:0:0:0: [sdb] Attached SCSI removable disk [2334597.548919] sd 1:0:0:0: [sdb]…
grep --color "$1"'\|$'Re: Grep flags – the good stuff
#33 grep -nRI foo ./
Sometimes I add `-i`Often I will add `-P` and encase the regex with single-quotes of course
Re: Grep flags – the good stuff
#34very frequently I want to chain grep things... I lean on the "|" operator for this, e.g. cat hello | grep foo | grep bar and it seems verbose. any tips?
With sed instead of grep , and probably not much better, but in a single command: cat hello | sed -n '/foo/{ /bar/{ /baz/ p }}'
Re: Grep flags – the good stuff
#35 LC_ALL=C grep -larP '\x1A\x2B\x3C\xFF'Re: Grep flags – the good stuff
#36Earlier quoted context omitted.
I always used "-e ^" the same way you're using a null string, to show all lines of the files, each prefixed with the path and filename. Are they equivalent or is there a caveat I should watch out for?
Zsh (and possibly other shells?) will expand a raw ^ into filenames. '' is a little shorter than '^' if you have to quote it.
Years ago--I don't if it's still the case--but the fish shell used ^ to redirect stderr.
EDIT: I remembered right:
https://web.archive.org/web/20111111003423/http://fishshell....
but it seems it's no longer the case:
fish> echo foo ^ bar
foo ^ bar
https://fishshell.com/docs/current/tutorial.html#pipes-and-r...Re: Grep flags – the good stuff
#37An amazing grep trick that I use all the time: The -e flag can be used to search for multiple terms. A blank -e will search for null. Thus: Lets assume we have a log file with a bunch of relevant stuff, I want to highlight my search term, BUT I also want to keep all the other lines around for context: $ dmesg ...SNIP... [2334597.539661] sd 1:0:0:0: [sdb] Attached SCSI removable disk [2334597.548919] sd 1:0:0:0: [sdb]…
Re: Grep flags – the good stuff
#38I think I have never used grep -r. I'm sure gnu grep has some way to specify which files to search, but why would I learn that syntax as well when I already know find, and exec works (exec + is much faster, but exec ; gets you the results too if your find lacks exec +).
Re: Grep flags – the good stuff
#39Re: Grep flags – the good stuff
#40 find . -type f -name \*txt \; | xargs -I{} -P24 bash -c "grep -Hi foo '{}' ; :"