PowerShell和CSV

时间:2016-07-20 18:56:06

标签: csv powershell

下午好。我希望我可以通过一些用于维护CSV文件的脚本来获得一些帮助。此文件的最大问题是日期格式更改通过添加到文件的系统。我的目标是能够运行此脚本,将所有日期更改为MM / dd / yyyy,将最旧的日期排序为最新(按日期),然后搜索重复的地址并保留最旧的日期。

我有一点开始,但我有点卡住了。我也做了很多(我相信的)很多不必要的文件创建,并且使这些事情过于复杂。我是PowerShell的新手,也是一般的脚本。

这是我的数据的样子(注意第一行没有引号,输出应该保持不变)。它是一个txt文件,但读起来像CSV。

dev_address,date_added,comments,user_id,dev_name
"00:01:02:03:04:0a","05/19/2014","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0b","05/19/2014","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0c","05/19/2013","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0d","2015/7/8","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0e","2016/06/03","NIC Manufacturer = Microsoft","bob","comp1"

这是我到目前为止所写的内容:

$devList = Import-Csv .\TestFile.txt

$a = Get-Content .\TestFile.txt -TotalCount 1

Add-Content -Path '.\foo2.txt' -Value "$a"

$countList = -1

ForEach($devList in $devList)
    {
        $devMAC = $devList.dev_address
        $devDate = $devList.date_added
        $devComm = $devList.comments
        $devUser = $devList.user_id
        $devMName = $devList.dev_name

        $devDate = ([datetime]($devDate)).ToString('MM/dd/yyyy')

        Add-Content -Path '.\foo2.txt' -Value """$devMAC"",""$devDate"",""$devComm"",""$devUser"",""$devMName"""
    }

Import-Csv '.\foo2.txt' | Sort-Object {[datetime]$_.date_added} | Export-Csv -NoTypeInformation '.\foo3.txt'

现在我知道这很麻烦。所以我会试着解释一下我在做什么。导入CSV文件。将CSV文件的第一行(标题)保存到变量中,以便稍后可以将其更改为最终文件。接下来,将每个记录的数据导入变量,以便将它们输入到最终文件中。我有点想到日期的事情。这似乎将日期更改为正确的格式,然后第2行到最后一行将其写入$ devList中每个条目的文件。最后一行是导入第二个文件,按日期排序并导出到第3个最终文件。大多数情况下都是冗没有计划有3个文件。

导入数据的最佳方法是什么,将日期格式化为MM / dd / yyyy,按最旧到最新排序,然后根据dev_address删除重复记录(但保留最旧的记录),然后导出到一个新文件,包括没有引号的CSV标题?

提前谢谢: - )

编辑:

抱歉,我应该包含所需的输出。说我有这个文件:

dev_address,date_added,comments,user_id,dev_name
"00:01:02:03:04:0a","05/19/2014","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0a","06/05/2014","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0c","04/20/2013","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0d","2015/7/8","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0e","2016/06/03","NIC Manufacturer = Microsoft","bob","comp1"

这是我想要的输出:

dev_address,date_added,comments,user_id,dev_name
"00:01:02:03:04:0c","04/20/2013","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0a","05/19/2014","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0d","07/08/2015","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0e","06/03/2016","NIC Manufacturer = Microsoft","bob","comp1"

正如您可以看到已修复和订购的日期,已删除冗余dev_address并保留最旧版本。顶线保持不变。

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的......你自己非常接近。 :)你的最终输出将在output.txt

顶行(标题)用双引号封装。请考虑坚持这一点,因为它将创建一个通用的“csv'不同cmdlet和/或其他进程之间的格式,以便将CSV文件识别为通用。

编辑:我之前按错误顺序排序,我调整了它:

$devices = Import-CSV 38488676.csv

foreach ($device in $devices)
{
    $date = $device.date_added -as [DateTime];
    $device.date_added = $date.ToString("M/d/yyyy")
    #this foreach could all be done in one line by itself...
    #but it's easier to visualize and understand when broken out more
}

$devices | Sort-Object {$_.date_added -as [datetime]} | Sort dev_address -Unique | Export-CSV -NoTypeInformation output.txt

这给了我以下输出:

"dev_address","date_added","comments","user_id","dev_name"
"00:01:02:03:04:0c","4/20/2013","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0a","6/5/2014","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0d","7/8/2015","NIC Manufacturer = Microsoft","bob","comp1"
"00:01:02:03:04:0e","6/3/2016","NIC Manufacturer = Microsoft","bob","comp1"