首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell:获取单元格,只从其中选择某些数据,并将其输出到新的csv中。

Powershell:获取单元格,只从其中选择某些数据,并将其输出到新的csv中。
EN

Stack Overflow用户
提问于 2015-04-22 13:47:11
回答 1查看 479关注 0票数 0

因此,我在一个列中选择数据时遇到了问题,该列中的数据比我需要的更多,并且只将这些数据输出到一个新的csv中。我有一个8列CSV,我只从其中选择2列,然后删除插件输出列中包含TCP的所有行。这件事很好,但我想不出下面的任务。

代码语言:javascript
复制
Import-csv c:\csv.csv | select "Plugin Output",Host | 
    Where-Object ($_."Plugin Output" -notmatch "tcp"| 
    export-csv -path C:\new.csv -NoTypeInformation

继续下一个任务。取单元说明“远程操作系统: Microsoft Windows 7专业可信度: 99方法: MSRPC远程主机正在运行”插件输出“中的Microsoft Windows 7 Professional”,并仅从其中选择Windows 7、Unix或Cisco。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-22 13:58:40

我已经更新了下面的代码,向您展示了如何实现这一点的两个示例,一个是一行,另一个是使用不同方法的多行格式。

代码语言:javascript
复制
#Define any functions first
function CheckOS ([string]$CellContent) {
    switch -Wildcard ($CellContent) {
        "*windows 7*" {"Windows 7"}
        "*unix*" {"Unix"}
        "*cisco*" {"Cisco"}
        #if id doesn't match anything, return the original
        default {return $CellContent}
    }
}

#Based on your example, this will be "Windows 7"
#$OS = CheckOS $Cell

#Method 1, in one line
Import-csv c:\csv.csv | select "Plugin Output",Host | 
    Where-Object ($_."Plugin Output" -notmatch "tcp") |
    foreach-object {$_."Plugin Output" = (CheckOS $_."Plugin Output")} |
    export-csv -path C:\new.csv -NoTypeInformation

<#

#Method 2, not in one line, not using a function or switch
$CSV = Import-csv c:\csv.csv | select "Plugin Output",Host | 
    Where-Object ($_."Plugin Output" -notmatch "tcp")

#iterate through each item in the CSV
foreach ($Cell in $CSV) {
    if ($Cell."Plugin Output" -like"*windows 7*") {
        $Cell = "Windows 7"
    } elseif ($Cell."Plugin Output" -like "*unix*") {
        $Cell = "Unix"
    } elseif ($Cell."Plugin Output" -like "*cisco*") {
        $Cell = "Cisco"
    }
}

#export the csv when done
$CSV | Export-Csv -path C:\new.csv -NoTypeInformation

#>

编辑:我最初犯了一些错误。首先,在方法1中,我把它设置为一个变量,这个变量是错误的,它是我大脑中遗留下来的,引发了一个不同的想法。在方法2中,我应该使用-like,而不是使用-contains 这是错误的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29799588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档