Live data from Hacker News

Grep flags – the good stuff

zwischenzugs.com

31–40 of 53 posts

Re: Grep flags – the good stuff

#31
post #3

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

or even something like --exclude={foo,ba[rz]} , probably getting the syntax wrong

Re: Grep flags – the good stuff

#32
post #4

An 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]…

I have this in my ~/bin, but I like your version even better!:

    grep --color "$1"'\|$'

Re: Grep flags – the good stuff

#34
post #20

very 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 }}'

with awk: <hello awk '/foo/ && /bar/ && /baz/'

Re: Grep flags – the good stuff

#35
It's weirdly difficult to get grep to search for fixed binary strings, with lots of gotchas if you don't understand grep internals. I still don't, but this is the best I have been able to do after knocking my forehead on three or four of said gotchas:

    LC_ALL=C grep -larP '\x1A\x2B\x3C\xFF'

Re: Grep flags – the good stuff

#36
post #24

Earlier 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.

> and possibly other shells?

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

#37
post #4

An 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]…

Since you're probably (or at least often) pipelining this into "less" assuming there is more than one screenful of lines, why not just use the regexp searching/highlighting built into less?

Re: Grep flags – the good stuff

#38
post #11

I 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 +).

Yep, find ... | xargs grep ... is something I use almost daily. I don't use -exec with find, mostly just because I learned the xargs way and it's habit.

Re: Grep flags – the good stuff

#40
Grep is nice and I've used it daily, but damn does it need multi threading! Especially for recursive greps. I find myself doing this a heck of a lot these days:

  find . -type f -name \*txt \; | xargs -I{} -P24 bash -c "grep -Hi foo '{}' ; :"
Post reply on HN