[英]Manipulating a string in a text file via PowerShell to return a specific value
I'm currently attempting to pull the version number out of an assemblyinfo.cs file and then append JUST the version number, 1.2.3.4, to another text file. 我目前正在尝试从Assemblyinfo.cs文件中拉出版本号,然后将版本号1.2.3.4附加到另一个文本文件中。
I've attempted to use the .split() method, but that only works if the item is already a string(from what I've read) and I pull the entirety of the string from the text file by using select-string 我尝试使用.split()方法,但是仅当该项目已经是一个字符串(根据我的阅读)并且我使用select-string从文本文件中提取整个字符串时,该方法才有效
Here's what I'm working with 这就是我的工作
$a = cat "c:\\path_to_file\\AssemblyInfo.cs" | $ a =猫“ c:\\ path_to_file \\ AssemblyInfo.cs” | select-string AssemblyVersion 选择字符串AssemblyVersion
Which will produce: [assembly: AssemblyVersion("5.7.4.1")] 它将产生:[assembly:AssemblyVersion(“ 5.7.4.1”)]
And then if I try to do $a.split(' " ') I will get 然后,如果我尝试执行$ a.split('“'),我会得到
Method invocation failed because [System.Object[]] doesn't contain a method named 'split'. 方法调用失败,因为[System.Object []]不包含名为“ split”的方法。 At line:1 char:21 + $versionstring.split <<<< (' " ') + CategoryInfo : InvalidOperation: (split:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound 在第1行:char:21 + $ versionstring.split <<<<('“')+ CategoryInfo:InvalidOperation:(split:String)[],RuntimeException + FullyQualifiedErrorId:MethodNotFound
Which as I said, is due to the fact that it's considered as an Object, not a string. 正如我所说,这是由于将其视为对象而不是字符串。
The result that I want is like this though: 我想要的结果虽然是这样的:
PS C:\> $a = '[assembly: AssemblyVersion("5.7.4.1")]'
$b = $a.split(' " ')
$b[2]
5.7.4.1
I'm assuming the issue is regarding the fact that I'm using CAT to display the contents and then pull the string, I'm not sure. 我不确定问题在于我正在使用CAT显示内容然后拉出字符串的事实,我不确定。
Select-String returns a MatchInfo object. Select-String返回MatchInfo对象。 The string value of the line is in the .line property of that object 该行的字符串值在该对象的.line属性中
$a.line.split(' " ')
should give you the result you're looking for. 应该给您您想要的结果。
Use 采用
$a | Get-Member
to see all the available properties that are returned. 查看返回的所有可用属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.