简体   繁体   中英

How to extract string separated by blank spaces in windows command line batch commands

I am trying to read the latest commit for a git remote repository using this command

git ls-remote https://repo.myrepository.com/scm/swc/project.git refs/heads/qa

it works fine and returns me something like this

5261626431661281d788382a1ed6ab1440fd93a8        refs/heads/qa

But I am not able to find online any way to extract only the commit hash from the returned string in command line

So basically I want to extract this string from the output of the command

5261626431661281d788382a1ed6ab1440fd93a8

I thought it would be very easy to find this information online, but the only answer I am finding everywhere is this

git ls-remote https://repo.myrepository.com/scm/swc/project.git refs/heads/qa | \ cut -f 1

But this does not work in windows command line, ofcourse because cut is not windows command.

Can anybody please help for windows command line version of it?

If you are calling this command from C# , you can process the output in C# :

if (lines.Count > 0) {
   sha = lines[0].Substring(0, 40)
}

I imagine you are working with Windows. If git was installed with the standard windows installer (as opposed to : compiled from source), it actually comes with the standard linux tools (grep, cut, ...).

It just doesn't update the global %PATH% in your system to point at these tools - the main known issue is : there is a standard windows tool named find , which does not behave as linux's find , updating the global %PATH% for everyone would break the scripts that rely on windows' version of find .

One possible way to use them :

  • find the directory which contains all the standard linux tools,
  • run your Process command with a PATH environment that contains this directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM