更新时间:2023-11-03 08:41:16
有很多方法可以选择csv的行并显示数据
There are many ways to select a Row of a csv and to present the data
为演示起见,我将内联csv与here字符串一起使用.
For demonstration I use an inline csv with a here string.
$Test = @"
Name;property;location
n1;13;computer
n2;65;computer
n3;12;tablet
n4;234;phone
n5;123;phone
n6;125;phone
"@ | ConvertFrom-Csv -delimiter ';'
> $test[0]
Name property location
---- -------- --------
n1 13 computer
> $test | where-object Name -eq 'n1'
Name property location
---- -------- --------
n1 13 computer
> $test | where-object Name -eq 'n1' | Select-Object Name,property
Name property
---- --------
n1 13
> $test | where-object Name -eq 'n1' | ForEach-Object {"Name:{0} has property: {1}" -f $_.Name,$_.property}
Name:n1 has property: 13
一旦导入了csv行内容,便会转换为对象
如果要获取与条件匹配的csv的原始行,请不要导入,但:
If you want to get the original row of the csv matching a criteria don't import but:
> Get-Content "C:\CSVFiles\test.csv" | Select-String '^n1'
n1;13;computer
^ n1
是在行首定位模式的正则表达式.
^n1
is a regular expression anchoring the pattern at line begin.
Select-String -Path "C:\CSVFiles\test.csv" -Pattern '^n1'
没有管道就一样