Replacing XML content with Regular Expressions and SED - regex

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)

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

Git Grep Multiple line 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

Nodemon ignore folder using regex expressions

In Nodemon ignore file I want to selectively ignore the folders from my main folder.
My folder structure is :
-modules
-accounts
-client
-angularfiles
-accounts.js
-repository
-accountrepository.js
-bankbranch
-client
-angularfiles
-bankbranch.js
-repository
-bankbranchrepository.js
In this hierarchy I want to ignore file paths "modules/accounts/client/angularfiles/" &
"modules/bankbranch/client/angularfiles/"
I have many more such modules. In this case I tried writing this regex to ignore based on the expression like so :
/modules\\\w*\\client\\angularfiles\\*/
Using the online regex matcher it matches this path :
\AngularJSApp\modules\accounts\client\angularfiles\bankbranch.js
but nodemon restarts when the file in client\angularfiles is changed.
Nodemon relies on file patterns rather than regular expressions for ignoring some files.
Try this instead:
nodemon --ignore 'modules/**/client/angularfiles/*'

Search and replace in *.aspx,*.ascx files when publishing a Webapplication

I'm attempting to replace a pattern in all my .aspx and .ascx file when I Publish my Webapplication.
When I am running the application locally, I don't care about the replace. But as soon as I need to Publish the solution I need a sequence of characters, let's say "ABC", replaced with "DEF" in all my .aspx and .ascx files.
How would I go about performing this?
You should create a separate script, that goes through your folder searching and loading all your .aspx and .ascx files, open them and replace all the needed stuff. I don't know how to do it in asp, but in actionscript it would look like fileText = fileText.replace(/ABC/g,"DEF");
perl -p -i -e 's/ABC/EDF/g' *.aspx
perl -p -i -e 's/ABC/EDF/g' *.ascx

ignoring folders in mercurial

Caveat:
I try all the posibilities listed here: How can I ignore everything under a folder in Mercurial.
None works as I hope.
I want to ignore every thing under the folder test. But not ignore srcProject\test\TestManager
I try
syntax: glob
test/**
And it ignores test and srcProject\test\TestManager
With:
syntax: regexp
^/test/
It's the same thing.
Also with:
syntax: regexp
test\\*
I have install TortoiseHG 0.4rc2 with Mercurial-626cb86a6523+tortoisehg, Python-2.5.1, PyGTK-2.10.6, GTK-2.10.11 in Windows
Try it without the slash after the caret in the regexp version.
^test/
Here's a test:
~$ mkdir hg-folder-ignore
~$ cd hg-folder-ignore
~/hg-folder-ignore$ echo '^test/' > .hgignore
~/hg-folder-ignore$ hg init
~/hg-folder-ignore$ mkdir test
~/hg-folder-ignore$ touch test/ignoreme
~/hg-folder-ignore$ mkdir -p srcProject/test/TestManager
~/hg-folder-ignore$ touch srcProject/test/TestManager/dont-ignore
~/hg-folder-ignore$ hg stat
? .hgignore
? srcProject/test/TestManager/dont-ignore
Notice that ignoreme isn't showing up and dont-ignore is.
Both cases worked for me (on linux and windows):
syntax: regexp
^backup/ #root folder
nbproject/ #any folder
or
syntax: glob
./backup/* #root folder
nbproject/* #any folder
However, it wasn't before I added a link to .hgignore file to .hgrc file in my repo:
[ui]
ignore = .hg/.hgignore
Also worth mentioning that mercurial ignores files that it is not currently tracking, which are those added before you configured it to ignore them. So, don't be put off by hg status saying some filed are M (modified) or ! (missing) in the folders that you have just added to the ignore list!
You can use zero-width negative look-ahead and look-behind assertions to specify that you want to ignore test only when it's not preceded by srcProject and not followed by TestManager:
syntax: regexp
(?<!srcProject\\)test\\(?!TestManager)
Mercurial uses Python regular expressions, so you can find more info on zero-width assertions in the Python docs: https://docs.python.org/library/re.html#regular-expression-syntax
Create .hgignore file under root directory of the repository
Now add the following contents in the file .
syntax: glob
bin/**
*.DS_Store
This will remove the bin directory and all the *.DS_Store files from the repository