PowerShell 从 csv 中提取特定列并存储在变量中 [英] PowerShell to extract specific column from csv and store in a variable
问题描述
我的 keys.csv 文件看起来像这样
My keys.csv file looks like this
PrjKey BldKey key
LS LOOKUPSNAP1 LS-LOOKUPSNAP1
LS LSUH3 LS-LSUH3
LSPERF LPMDS0 LSPERF-LPMDS0
LSPERF LSP0 LSPERF-LSP0
我想提取值列key"并存储在一个变量中.我试过了
I want to extract the values column "key" and store in a variable. I tried this
Import-Csv .\keys.csv | select key | ft -hide
输出顶部有一个空行.我怎样才能消除它.我只希望将这 4 个值存储在一个变量中"是的,我不想再次将输出保存为 csv.
the output has a blank line at the top. How can I eliminate it. I only want those 4 values to be "stored in a variable" Yes I don't want to save the output as a csv again.
有人可以帮忙吗?
推荐答案
您使用 Select-Object
来获得您想要的一个属性的想法是正确的.您遇到的两个问题是 Select-Object key
返回和带有 key 属性的对象数组,当看起来您只想要字符串数组键时.当您想要保存数据或以其他形式使用数据时,永远不要使用 Format-cmdlets
.它们为了在屏幕上显示数据而销毁对象.就这么说吧..
You had the right idea of using Select-Object
to get the one property you want. The two issues you had was that Select-Object key
returns and object array with a key property when it looks like you just want string array of keys. Also never use Format-cmdlets
when you want to save the data or use it in another form. They destroy objects for the purpose of display data on the screen. So that all being said..
$keys = Import-Csv .\keys.csv | select -ExpandProperty key
取决于您的 PowerShell 版本
Depending on your PowerShell version
$keys = (Import-Csv .\keys.csv).key
这篇关于PowerShell 从 csv 中提取特定列并存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!