简体   繁体   English

在 powershell 中导入和导出 csv 文件

[英]import and export csv file in powershell

i have a csv file "emp.csv" with following information.我有一个 csv 文件“emp.csv”,其中包含以下信息。

empid   status
1   t
2   a
3   t
4   t
5   a

I need empid and status of employees having status="t" and the output should be in "res.csv" file我需要 empid 和 status="t" 的员工的状态,并且 output 应该在“res.csv”文件中

For this, you'll first want to import the existing data with Import-Csv - but since your file is technically a TSV (tab-separated instead of comma-separated), make sure you specify this with -Delimiter "`t" :为此,您首先要使用Import-Csv导入现有数据 - 但由于您的文件在技术上是 TSV(制表符分隔而不是逗号分隔),请确保您使用-Delimiter "`t"指定它:

$employeeStatusData = Import-Csv .\path\to\emp.csv -Delimiter "`t"

Import-Csv will parse the input file and store the resulting objects in $employeeStatusData . Import-Csv将解析输入文件并将生成的对象存储在$employeeStatusData中。 To filter these object based on the value of the status property/column, use Where-Object :要根据status属性/列的值过滤这些 object,请使用Where-Object

$filteredEmployeeData = $employeeStatusData |Where-Object status -eq 't'

Now that you have the relevant rows selected, you can export the data again with Export-Csv :现在您已经选择了相关的行,您可以使用Export-Csv再次导出数据:

$filteredEmployeeData |Export-Csv .\path\to\res.csv -Delimiter "`t" -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM