如何在Powershell中将文本文件转换为CSV? [英] How to convert text file to csv in powershell?

查看:214
本文介绍了如何在Powershell中将文本文件转换为CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件.每行记录一个事件,并用"|"分隔字段. 是否可以使用"get-content event.log | export-csv event.csv"之类的cmdlet将文本文件转换为csv文件?

I have a text file. Each line record an event and the fields are delimited by '|'. Is it possible using some cmdlet like "get-content event.log | export-csv event.csv" to convert text file to csv file?

xxx(1365)|2016-09-29 06:00:00.0|2016-09-29 06:30:00.0|
bbb(110)|2016-09-29 06:30:00.0|2016-09-29 07:00:00.0|
ccc(5243)|2016-09-29 07:00:00.0|2016-09-29 07:30:00.0|
ddd(1950)|2016-09-29 07:30:00.0|2016-09-29 08:00:00.0|
eee(10)|2016-09-29 08:00:00.0|2016-09-29 09:00:00.0|
fff(464)|2016-09-29 09:00:00.0|2016-09-29 10:00:00.0|
dddd(874)|2016-09-29 10:00:00.0|2016-09-29 11:00:00.0|
ggggg(6)|2016-09-29 11:00:00.0|2016-09-29 12:00:00.0|
kkkk(272)|2016-09-29 12:00:00.0|2016-09-29 12:30:00.0|

推荐答案

Import-Csv cmdlet允许您指定分隔符

The Import-Csv cmdlet allows you to specify a delimiter

$file = Import-Csv .\event.log -Delimiter '|'

因此,就您而言,它可以很简单

so in your case, it can be as simple as

Import-Csv .\event.log -Delimiter "|" | Export-Csv .\event.csv -NoTypeInformation

这篇关于如何在Powershell中将文本文件转换为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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