使用 powershell 解析逗号分隔的文件
我有一个包含多行的文本文件,每行都是逗号分隔的字符串。每行的格式为:
Bitness
和 OSType
是可选的。
例如,文件可以是这样的:
Name1, Value1, X64, Windows7
Name2, Value2, X86, XP
Name3, Value3, X64, XP
Name4, Value3, , Windows7
Name4, Value3, X64 /*Note that no comma follows X64 */
....
....
我想将每一行解析为 4 个变量并对其执行一些操作。这是我使用的 PowerShell 脚本。
Get-Content $inputFile | ForEach-Object {
$Line = $_;
$_var = "";
$_val = "";
$_bitness = "";
$_ostype = "";
$envVarArr = $Line.Split(",");
For($i=0; $i -lt $envVarArr.Length; $i++) {
Switch ($i) {
0 {$_var = $envVarArr[$i].Trim();}
1 {$_val = $envVarArr[$i].Trim();}
2 {$_bitness = $envVarArr[$i].Trim();}
3 {$_ostype = $envVarArr[$i].Trim();}
}
}
//perform some operation using the 4 temporary variables
}
但是,我想知道是否可以在 PowerShell 中使用正则表达式来执行此操作。您能提供执行此操作的示例代码吗?请注意,每行中的第三个和第四个值可以选择为空。
I have a text file which contains several lines, each of which is a comma separated string. The format of each line is:
<Name, Value, Bitness, OSType>
Bitness
and OSType
are optional.
For example the file can be like this:
Name1, Value1, X64, Windows7
Name2, Value2, X86, XP
Name3, Value3, X64, XP
Name4, Value3, , Windows7
Name4, Value3, X64 /*Note that no comma follows X64 */
....
....
I want to parse each line into 4 variables and perform some operation on it. This is the PowerShell script that I use..
Get-Content $inputFile | ForEach-Object {
$Line = $_;
$_var = "";
$_val = "";
$_bitness = "";
$_ostype = "";
$envVarArr = $Line.Split(",");
For($i=0; $i -lt $envVarArr.Length; $i++) {
Switch ($i) {
0 {$_var = $envVarArr[$i].Trim();}
1 {$_val = $envVarArr[$i].Trim();}
2 {$_bitness = $envVarArr[$i].Trim();}
3 {$_ostype = $envVarArr[$i].Trim();}
}
}
//perform some operation using the 4 temporary variables
}
However, I wanted to know if it is possible to do this using regex in PowerShell. Would you please provide sample code for doing that? Note that the 3rd and 4th values in each line can be optionally empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
Import-Csv
不是更好吗 哪一个可以为您完成这一切(并且更可靠)?Wouldn't it be better to use
Import-Csv
which does all this (and more reliably) for you?正如 Tim 建议的那样,您可以使用 Import-Csv。不同之处在于 Import-Csv 从文件中读取。
As Tim suggests, you can use use Import-Csv. The difference is that Import-Csv reads from a file.
比糖蜜慢,但在花了 20 年时间拼凑出十几个或更多的部分解决方案后,我决定彻底解决这个问题。当然,随着时间的推移,现在可以使用各种解析器库。
Slower than molasses but after spending 20 years cobbling together a dozen or more partial solutions I decided to tackle it definitively. Of course in the course of time all sorts of parser libraries are now available.
您可以使用
Import-Csv
cmdlet 的-Header
参数为导入的文件指定备用列标题行:You can specify an alternate column header row for the imported file file with the
-Header
parameter of theImport-Csv
cmdlet: