Greg McGuffey
Greg McGuffey

Reputation: 3326

How to extract text from line with tools provided by Git (on windows)

I'm trying to extract some text using a regex using the tools provided by Git on windows. Specifically, I'm trying to extract part of the AssemblyVersion and place it in a variable within a hook script. I.e. I have the following in my AssemblyInfo.cs file:

[assembly: AssemblyVersion( "1.1.1.0" )]

And I want to extract "1.1.1". I have a regex that does that but I can't get any of the tools provided by Git to work.

awk match: I tried using awk (gawk) and the match with the three argument version, where it fills an array with captured groups. Apparently in the version provided with git, there is no version of match that supports 3 arguments.

grep -o: I tried grep -o, which I think is supposed print out the captured groups, but the version with git does not have a -o option.

So, two questions: 1. Is there any way to capture some text from a regex and put into a variable in a hook script (when on windows)? 2. Is there someplace to figure out what is possible with the versions of the GNU tools provided by git on windows?

Upvotes: 0

Views: 123

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174874

You could use awk.

$ echo '[assembly: AssemblyVersion( "1.1.1.0" )]' | awk -F'"' '{print $2}'
1.1.1.0
$ echo '[assembly: AssemblyVersion( "1.1.1.0" )]' | awk -F'"' '/AssemblyVersion/{print $2}'
1.1.1.0
$ echo '[assembly: AssemblyVersion( "1.1.1.0" )]' | awk -F'"' '/AssemblyVersion/{split($2,a,".");print a[1]"."a[2]"."a[3]}'
1.1.1

Upvotes: 1

Related Questions