Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `git grep --not`

Tags:

git

grep

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}'
like image 774
Sam Houston Avatar asked May 18 '19 22:05

Sam Houston


People also ask

How do I grep a branch in git?

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.

What files are searchable by git grep?

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.

Why is git grep so fast?

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.

Which statement is the best comparison between git grep and grep?

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.


2 Answers

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

like image 75
torek Avatar answered Sep 20 '22 16:09

torek


You can also use awk for this problem.

awk '/<pattern>/ && length($2) < 200'

like image 36
Abdul Fatah Avatar answered Sep 21 '22 16:09

Abdul Fatah