Git Grep Multiple line Regex - regex

I wrote a regex to find any file containing space Word [ and here is the regex.
^\s+Session\[
and I want to use this regex in git grep,
so I set up a file in my repo that matched the regex and runs it.
here is what I run
git grep '^\s+Word\[' -- '*.cs'
but it returns nothing. I'm really new to git and regex any reference or suggestion to solve this problem?

It looks like you are trying to use Perl regular expressions syntax with git grep. Just add -P (perl-regexp) option and it will work.
UPD: for those who comes here to find about multi line patterns to git grep (just like me): Git Grep Multiple Words on Multiple Lines

Related

Find and replace pattern in large number of files

I want to replace text in about 80.000 log files using a regex. I love the batch search and replace of VSCode. I was unable to do this with VSCode, because it did not seem to handle this amount of data well. Any suggestion how I could do this with VSCode? Are there suggestions for alternatives?
Instead of depending on a GUI based tool, it might be easier to for a CLI tool for this.
If you're using Linux, or willing to install any of the tools like sed and find if you're on Windows then it should be relatively simple.
You can use sed which is a command line tool on all (or at least most) distributions of Linux, and can be installed on Windows.
Usage (for this use case):
sed -i s/{pattern}/{replacement}/g {file}
Use sed to replace the matched pattern with a replacement, using the global modifier to match all results, and the file to do the replacement and overwrite.
To target all files in a directory you can do:
find -type f -name "*.log" exec sed -i s/{pattern}/{replacement}/g {};
Find items recursively starting from the current directory where it's type is file, and it has a name ending with .log. Then use sed to replace the pattern with the contents you want for each matched file.
You can find how to get tools like sed and find for Windows on the following question:
https://stackoverflow.com/a/127567/6277798

Replacing XML content with Regular Expressions and SED

I have a project that's effectively 2 maven projects. In the "child" maven project I have a property that references the version in the parent project:
<myparent.project.version>15.0.0-SNAPSHOT</myparent.project.version>
What I'm trying to do is bash script a few common commands together, i.e. when creating a release branch I want to create one of the parent maven project, then update the child maven parent project but Im missing how to update this value:
I'm hoping to get something along the lines of working:
where newVersion = 16.0.0
$ sed -i "s/\<myparent.project.version\>[0-9.]+-SNAPSHOT\<\/myparent.project.version\>/<myparent.project.version>$newVersion-SNAPSHOT<\/myparent.project.version>/g" pom.xml
I have checked the regex (\<myparent.project.version\>[0-9.]+-SNAPSHOT\<\/myparent.project.version\>) with https://regex101.com/ and it matches the line I want to replace in my pom.xml file, but for some reason, I can't get the sed to work correctly.
Any ideas?
You should use an xml parser like XMLStarlet. That said, for that simple substitution, you can try this GNU sed:
sed "s/<myparent\.project\.version>[0-9.]\+-SNAPSHOT<\/myparent\.project\.version>/<myparent.project.version>$newVersion-SNAPSHOT<\/myparent.project.version>/g" file
You must escape the one or more quantifier: \+ and the dot: \. but not the < nor >(\< and \> are used for word boundaries with GNU sed)

Git commit uses regular expressions or globs?

Is it possible to use regular expressions in, e.g., git commit ".*my_file.*" ?
I tried, and it seems to only interpret these as globs. I also tired a regex flag:
git commit -regex ".*my_file.*"`
Throws an error.
Does anyone know of a way to combine regular expressions with Git commands?
The best way I can think of to do this is using the find command. For example, if you want only python files:
find -type f -regex ".*\.py$" -exec git commit {} -m "committing only and all python files" \;
Can anyone else think of something less unwieldy?
Not with Git itself. Git just receives a list of files passed from the shell, so it would be up to your shell to do regular expression matching for files. I do not think bash can do this, but other shells may be able to.
As mentioned by mipadi, arguments can be generated by the shell to produce the input for git.
For example, ls can be used in combination for this. Let's say I've got a git init-ed directory with the following files:
.git/
my_new_project_file.py
my_older_project_file.py
some_other_file.py
And I want to add everything but some_other_file.py. To do this:
ls my*project*.py | xargs git add
Checking my git status will show that both my_new_project_file.py and my_older_project_file.py have been staged, while some_other_file.py has been ignored.
n.b. ls doesn't support regex; just globbing.
As pointed out by Candic3, git supports globs:
git add *my_file*

Search filenames with regex

Is there any way to do something like git log <path>, but instead of path using a regex? I want to search commits containing files, whose filenames match a given pattern...
... and while we're at it: Is there also a way to do a git status / git diff only for filenames matching a given pattern?
Thanks in advance!
EDIT: I would be terrific if any way to do it, would also work for Git v1.7.1.
As far as a pure git solution goes and I'm aware of the only option to match specific file patterns is to use a glob.
git log -- '*.json'
Will give you all files which contain changes to a json file. The same can be done for git status.
On the other hand it's quite easy to search for regular expressions in the diff or the commit message. git log offers a --grep option to search for matches in the commit message and a -S option to search for strings.
Take a look at this question for further details.
For a simple pattern you could try, for example:
find . -name "*.c" | xargs git log
For a full-blown regex you can use:
find . | grep "REGEX" | xargs git log
If you need previously deleted files to be included in the output, you can use
git log --all --pretty=format: --name-only --diff-filter=A | sort -u | grep "REGEX" | xargs git log --
The first part of the above command, which finds all files that were ever in git, was lifted from an answser to this other question.
Thanks to your answers (especially Greg and Michael) I developed a way myself. (I hope this proves viable):
git log --name-only --pretty="format:"|sort -u|egrep '<REGEX>'|xargs git log --
Can you do something like:
git log | grep [string_to_look_for]

how can i use regex with locate command in linux

I want to use the locate command with regex but i am not able to use it.
I want to find pip file which is in /usr folder. i am trying this
locate -r "/usr/*pip"
To use globbing characters in your query you shouldn't specify regex (as you do with -r option), so just do:
locate "/usr/*pip"
From the man page:
If --regex is not specified, PATTERNs can contain globbing characters.
If any PATTERN contains no globbing characters, locate behaves as if
the pattern were *PATTERN*.
I would do so: locate -r '/usr/.*pip'