使用Powershell按列拆分csv文件 [英] Split csv file by columns using Powershell

查看:121
本文介绍了使用Powershell按列拆分csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 powershell 的新手,但这看起来是完成此任务的最佳工具.有一个如下所示的 csv 文件:

I am a novice at powershell but this looks to be the best tool to do this task. have a csv file that looks like this:

Date,mary1,mary2,maryr3,mary4
9/01/2011 1:00,1.39,3.43,3.29,1.83
9/01/2011 3:00,0,0,0,0
9/01/2011 4:00,0,0,0,0
9/01/2011 5:00,0,0,0,0
9/01/2011 6:00,1.91,0.07,0.09,0.09
9/01/2011 7:00,5.57,2.01,2.11,3.4
9/01/2011 8:00,0.53,0.41,0.35,2.01
9/01/2011 9:00,4.72,0.15,0.21,3.15
9/01/2011 10:00,0.21,0,0.49,0.72
9/01/2011 11:00,2.44,0.77,2.06,1.7
9/01/2011 12:00,0,4.91,5.21,1.98

这是逗号分隔的.我试图根据标题列将其拆分为包含一列的多个文本文件.

This is comma delimited. I am trying to split this into multiple text files containing one column, based on the header column.

实际上我会有多个列,所以我想要一个更通用的代码来处理更多的行和列.

In reality I will have multiple columns, so I would like to have a more generic code to deal with more rows and columns.

对此有什么想法吗?我可以使用 split 函数并取出单个列,但我对 powershell 的了解还不允许我进行多次提取.

Any thoughts on that? I am able to use the split function and get individual columns out, but my knowledge of powershell yet does not allow me to do the multiple extraction.

任何帮助将不胜感激.

推荐答案

尝试:

$a = Import-Csv .\myfile.csv
$b = $a[0] | Get-Member | ? { $_.membertype -eq 'noteproperty'} | select name
$b | % { $a | ft -Property $_.name | out-file "$($_.name).txt"  }

这篇关于使用Powershell按列拆分csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆