I would like to run a git grep
command for a <pattern>
, but exclude lines which are over 200 characters.
I have managed to achieve this with the command:
git grep <pattern> | grep -Ev '.{200}'
If possible, I would like to run this as one command (without a pipe).
I thought I would be able to make use of the git grep
--not
flag for this, but am running into difficulty.
Here is my attempt:
git grep -e <pattern> --not -e '.{200}'
I have tried different combinations of the above command, but haven't gotten the desired output.
Any pointers on what I should try next would be greatly appreciated, thanks in advance for the help.
Solution:
(thanks to @torek)
git grep -E -e one --and --not -e '.{200}'
Any git repository contains many files, folders, branches, tags, etc. Sometimes it requires searching the particular content in the git repository using a regular expression pattern. `git grep` command is used to search in the checkout branch and local files.
Git ships with a command called grep that allows you to easily search through any committed tree, the working directory, or even the index for a string or regular expression.
GNU grep is fast because it AVOIDS LOOKING AT EVERY INPUT BYTE. GNU grep is fast because it EXECUTES VERY FEW INSTRUCTIONS FOR EACH BYTE that it does look at.
The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory. So far so similar; either one could be better depending on what you want to achieve.
The description of the boolean operators for git grep
reads:
--and
--or
--not
( … )
Specify how multiple patterns are combined using Boolean expressions.--or
is the default operator.--and
has higher precedence than--or
.-e
has to be used for all patterns.
You used:
git grep -e <pattern> --not -e '.{200}'
so you did write -e
for each pattern; but you did not pick one of --and
or --or
, so you got the default. Which one is the default? (Read the quoted text above.) If you ask for things that match some pattern or are not at least 200 characters, will something that's 12 characters long be selected? It's not at least 200, so yes, it will be selected. What about something that's both 234 characters and matches? Well, it matches, so yes, it will be selected. The only things that will be rejected are those that both don't match and are 200 characters or longer.
Note that git grep
itself also defaults to basic regular expressions, unless you use a command line option or configure it to use extended or Perl regular expressions. So you probably want -E -e <pattern> --and --not -e '.{200}'
here. See also https://en.wikipedia.org/wiki/Regular_expression
You can also use awk
for this problem.
awk '/<pattern>/ && length($2) < 200'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With