TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Grep flags – the good stuff

87 pointsby U1F984over 3 years ago

21 comments

nickcwover 3 years ago
My favorite feature is:<p><pre><code> -P, --perl-regexp Interpret I&lt;PATTERNS&gt; as Perl-compatible regular expressions (PCREs). This option is experimental when combined with the -z (--null-data) option, and grep -P may warn of unimplemented features. </code></pre> As everything (python, Go, javascript, etc, etc) uses perl regexps now-a-days and I can never remember which things I need to escape for old gods regexp.
评论 #30181396 未加载
评论 #30188217 未加载
评论 #30185602 未加载
mmh0000over 3 years ago
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:<p>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:<p><pre><code> $ dmesg ...SNIP... [2334597.539661] sd 1:0:0:0: [sdb] Attached SCSI removable disk [2334597.548919] sd 1:0:0:0: [sdb] 57280429 512-byte logical blocks: (29.3 GB&#x2F;27.3 GiB) [2334597.761895] sd 1:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn&#x27;t support DPO or FUA [2334597.761900] sdb: detected capacity change from 0 to 57280429 [2334597.772736] sdb: [2334631.115664] sdb: detected capacity change from 57280429 to 0 ...SNIP... </code></pre> A simple grep, will only return the selected lines:<p><pre><code> $ dmesg | grep capacity [2334597.761900] sdb: detected capacity change from 0 to 57280429 [2334631.115664] sdb: detected capacity change from 57280429 to 0 </code></pre> But I want all lines:<p><pre><code> $ dmesg | grep --color -e capacity -e &#x27;&#x27; ...SNIP... [2334597.539661] sd 1:0:0:0: [sdb] Attached SCSI removable disk [2334597.548919] sd 1:0:0:0: [sdb] 57280429 512-byte logical blocks: (29.3 GB&#x2F;27.3 GiB) [2334597.761895] sd 1:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn&#x27;t support DPO or FUA *[2334597.761900] sdb: detected capacity change from 0 to 57280429* [2334597.772736] sdb: *[2334631.115664] sdb: detected capacity change from 57280429 to 0* ...SNIP... </code></pre> The null trick also works well on directories with many small files, like <i>&#x2F;proc&#x2F;</i> or <i>&#x2F;sys&#x2F;</i>. Say, for example, you wanted to get the filename and value of each file:<p><pre><code> $ grep -R &#x27;&#x27; &#x2F;sys&#x2F;module&#x2F;iwlwifi&#x2F;parameters&#x2F; &#x2F;sys&#x2F;module&#x2F;iwlwifi&#x2F;parameters&#x2F;nvm_file:(null) &#x2F;sys&#x2F;module&#x2F;iwlwifi&#x2F;parameters&#x2F;debug:0 &#x2F;sys&#x2F;module&#x2F;iwlwifi&#x2F;parameters&#x2F;swcrypto:0 &#x2F;sys&#x2F;module&#x2F;iwlwifi&#x2F;parameters&#x2F;power_save:N &#x2F;sys&#x2F;module&#x2F;iwlwifi&#x2F;parameters&#x2F;lar_disable:N ...SNIP...</code></pre>
评论 #30181652 未加载
评论 #30184832 未加载
评论 #30183459 未加载
asicspover 3 years ago
&gt;<i>The -I flag only considers text files. This radically speeds up recursive greps.</i><p>I use ripgrep when I need better speed. I&#x27;ve pretty much switched to ripgrep these days, but still use GNU grep when I&#x27;m answering questions on stackoverflow, reddit, etc.<p>&gt;<i>ABC flags</i><p>Good to also know about `--group-separator` and `--no-group-separator` when there are multiple non-contiguous matches. Helps to customize the separator or remove them altogether. Sadly, these options are still not explained in `man grep` on Ubuntu. You&#x27;ll have to use `info grep` or the online manual to find them.<p>Options I use often that is not mentioned in the article:<p>* `-c` to count the number of matches<p>* `-F` for fixed string matching<p>* `-x` to match whole lines<p>* `-P` for PCRE (as mentioned in many comments here)<p>* `--color=auto` this is part of command name alias, so it is always used<p>I wrote a book as well on &quot;GNU grep and ripgrep&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;learn_gnugrep_ripgrep" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;learn_gnugrep_ripgrep</a> Free to read online.
CalChrisover 3 years ago
I have a shell alias&#x2F;function variations of which I&#x27;ve used for decades. This is the zsh version:<p><pre><code> function fvi { grep -rl $1 . | xargs nvim +&#x2F;$1 } </code></pre> It greps a directory recursively and opens files which have a pattern and puts the pattern in the search buffer.
评论 #30189587 未加载
tptacekover 3 years ago
Honorable mention for `-q`, which is useful in shell scripts when you don&#x27;t want the output, just the result code.
评论 #30183162 未加载
fomine3over 3 years ago
Here&#x27;s how GNU grep detect binary files, good to know sometimes: <a href="https:&#x2F;&#x2F;unix.stackexchange.com&#x2F;a&#x2F;276028" rel="nofollow">https:&#x2F;&#x2F;unix.stackexchange.com&#x2F;a&#x2F;276028</a>
js2over 3 years ago
In my PATH I have this script as git-gsr, which I can call as &quot;git gsr&quot;.<p><pre><code> #!&#x2F;bin&#x2F;sh usage () { cat &gt;&amp;2 &lt;&lt;&#x27;__USAGE__&#x27; usage: git gsr [-P | --perl-regexp] &lt;old&gt; &lt;new&gt; [paths...] replace all occurrances of &lt;old&gt; with &lt;new&gt; optionally limited to &lt;paths...&gt; (as interpreted by git grep) -P, --perl-regexp interpret &lt;old&gt; as perl regular expression; default is to treat it as a fixed string. __USAGE__ exit 1 } pattern=&#x27;-F&#x27; perl=&#x27;BEGIN {($old, $new) = (shift, shift)} s&#x2F;\Q$old\E&#x2F;$new&#x2F;g&#x27; case &quot;$1&quot; in -P|--perl-regexp) shift pattern=&#x27;-P&#x27; perl=&#x27;BEGIN {($old, $new) = (shift, shift)} s&#x2F;$old&#x2F;$new&#x2F;g&#x27; ;; -*) usage ;; esac test $# -lt 2 &amp;&amp; usage old=$1; new=$2; shift; shift git grep -l -z $pattern &quot;$old&quot; -- &quot;$@&quot; | xargs -0 perl -pi -e &quot;$perl&quot; &quot;$old&quot; &quot;$new&quot;</code></pre>
pavonover 3 years ago
Learning about -o has decreased my use of sed considerably. Where I used to use:<p><pre><code> sed -n &#x27;s&#x2F;.*\(pattern\).*&#x2F;\1&#x2F;p&#x27; </code></pre> it can instead simply be:<p><pre><code> grep -o &#x27;pattern&#x27; </code></pre> The -w flag is new to me today - excited to save still more keystrokes!
aidenn0over 3 years ago
I think I have never used grep -r. I&#x27;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 +).
评论 #30181323 未加载
评论 #30184880 未加载
throwawayboiseover 3 years ago
It&#x27;s good to be aware that gnu grep has a lot more features than &quot;unix&quot; grep, so if you find yourself on a BSD system a lot of this stuff doesn&#x27;t work.
simon04over 3 years ago
<a href="https:&#x2F;&#x2F;explainshell.com&#x2F;explain?cmd=grep+-rilIv" rel="nofollow">https:&#x2F;&#x2F;explainshell.com&#x2F;explain?cmd=grep+-rilIv</a>
评论 #30183077 未加载
jjoonathanover 3 years ago
It&#x27;s weirdly difficult to get grep to search for fixed binary strings, with lots of gotchas if you don&#x27;t understand grep internals. I still don&#x27;t, but this is the best I have been able to do after knocking my forehead on three or four of said gotchas:<p><pre><code> LC_ALL=C grep -larP &#x27;\x1A\x2B\x3C\xFF&#x27;</code></pre>
评论 #30185913 未加载
chapsover 3 years ago
Grep is nice and I&#x27;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:<p><pre><code> find . -type f -name \*txt \; | xargs -I{} -P24 bash -c &quot;grep -Hi foo &#x27;{}&#x27; ; :&quot;</code></pre>
milliamsover 3 years ago
I have problems remembering the mnemonic for -A and -B. I can&#x27;t get it straight whether it&#x27;s &quot;before&quot; and &quot;after&quot; or &quot;above&quot; and &quot;below&quot;. I always just try one then the other!
评论 #30180442 未加载
评论 #30180864 未加载
评论 #30180841 未加载
评论 #30182895 未加载
dcassettover 3 years ago
I looked for an option to specify a filelist (like ctags -L file) but didn&#x27;t seem to find one. I&#x27;m assuming others are happy with using xargs or `cat file`, but it would seem like a useful feature.
waynesonfireover 3 years ago
very frequently I want to chain grep things... I lean on the &quot;|&quot; operator for this, e.g. cat hello | grep foo | grep bar and it seems verbose. any tips?
评论 #30181299 未加载
评论 #30181522 未加载
评论 #30187816 未加载
inetknghtover 3 years ago
My grep is almost always:<p><pre><code> grep -nRI foo .&#x2F; </code></pre> Sometimes I add `-i`<p>Often I will add `-P` and encase the regex with single-quotes of course
zwiebackover 3 years ago
Ah, learned the difference between -i and -I.<p>I also like --include and --exclude, especially since it allows regex for which files to look at
评论 #30181713 未加载
declnzover 3 years ago
I feel `grep -e` (at least) should have been the default in retrospect<p>Living without those few &quot;extensions&quot; feels... empty
notatoadover 3 years ago
i&#x27;ve had this stuck to my office wall for a while, and i&#x27;ve internalized most of it by now but it&#x27;s still great<p><a href="https:&#x2F;&#x2F;twitter.com&#x2F;b0rk&#x2F;status&#x2F;991880504805871616" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;b0rk&#x2F;status&#x2F;991880504805871616</a>
beembeemover 3 years ago
&quot;-Irs&#x2F;-Iirs --color=always&quot; is my standard set of flags<p>-l&#x2F;-h&#x2F;-v&#x2F;-o show up every now and then