1

我再次希望我可以在尝试做一些 Windows 批处理的东西时使用 bash。什么是这个 Bash 表达式的短的 windows 批处理等价物?

$ var="EIPADDRESS=192.168.20.139"; IPAddress="${var##*=}" ; echo "IP: ${IPAddress}"
IP: 192.168.20.139

上面删除了字符串中直到并包括最后一个“=”实例的所有内容。

我想出了一些去掉第一个n字符的东西,但我宁愿不依赖先验的字符串形式:

set var="EIPADDRESS=192.168.20.139"
set IPAdress=%var:~11%
echo IP: %IPAddress%

无论提供什么解决方案,它都必须与 Windows 7 的 vanilla 安装一起使用:所以请不要使用第三方工具。

4

3 回答 3

2

You can do this in PowerShell, which is installed on a vanilla Win7 install:

> $var = "EIPADDRESS=192.168.20.139"
> $var -match "(\d{1,3}\.){3}\d{1,3}"
> $ip = $Matches[0]

That is how you would run it at the command prompt. Creating a .ps1 file would be very similar to what is written here.

于 2013-05-15T18:26:21.023 回答
2

试试这个(是的,纯Batch):

(for %i in (EIPADDRESS=192.168.20.139) do @echo %i)|more +1
于 2013-05-15T18:34:51.197 回答
0

电源外壳:

$var = "EIPADDRESS=192.168.20.139"
$ipAddress = $var | select-string '(\d{1,3}\.){3}\d+' | foreach-object {
  $_.Matches[0].Groups[0].Value
}
"IP is $ipAddress"

账单

于 2013-05-15T18:27:20.207 回答