POWERSHELL - 添加列 + 更新几行

2024-01-09

我是 PowerShell 新手。

我目前正在尝试合并两个不同的 csv:

  • 一个有 7 列和 31 行
  • 另一个有3列,大约10行
  • 他们只有 PC 列是共同的,其余的我必须添加列

我尝试过几种操作,但没有成功。 这是 powershell 控制台上第一个 csv 的开头:

1st CSV https://i.stack.imgur.com/nPvq9.png

PS Z:\Partage> $CSVInitiale | Format-Table

PC              Processeur                               Taille_de_la_RAM Capacite_carte_reseau Taille_du_dique_dur Systeme_d_exploitation
--              ----------                               ---------------- --------------------- ------------------- ----------------------
ADSERVER        Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB             1 GB                  60,00 GB            Microsoft Windows Server 2016 Datacenter
PCW10           Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB             1 GB                  60,00 GB            Microsoft Windows 10 ?ducation
PCW7            Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 1 GB             1 GB                  80,00 GB            Microsoft Windows 7 Professionnel
User1_PCW10     Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User2_PCW7      Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB             200 MB                250 GB              Microsoft Windows 7 Professionnel
User3_PCW10     Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB            2 GB                  1500 GB             Windows 10 ?ducation
User4_PCW95     Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB             100 MB                120 GB              Windows 95
User5_PCW7      Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB            1 GB                  900 GB              Windows 7 Professionnel
User6_PCW10     Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User7_PCWVista  Intel(R) Core(TM) i5-1900K CPU @ 1.60GHz 1 GB             50 MB                 120 GB              Windows Vista Professionel
User8_PCW10     Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User9_PCW7      Intel(R) Core(TM) i5-8900K CPU @ 3.60GHz 16 GB            1 GB                  900 GB              Windows 7 Professionnel
User10_PCW7     Intel(R) Core(TM) i3-5900K CPU @ 2.60GHz 6 GB             500 MB                200 GB              Windows 7 Professionnel
User11_PCW10    Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User12_PCW7     Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB             200 MB                250 GB              Microsoft Windows 7 Professionnel
User13_PCW10    Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB            2 GB                  1500 GB             Windows 10 ?ducation
User14_PCW95    Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB             100 MB                120 GB              Windows 95
User15_PCW7     Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB            1 GB                  900 GB              Windows 7 Professionnel
User16_PCW10    Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User17_PCWVista Intel(R) Core(TM) i5-1900K CPU @ 1.60GHz 1 GB             50 MB                 120 GB              Windows Vista Professionel
User18_PCW10    Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User19_PCW7     Intel(R) Core(TM) i5-8900K CPU @ 3.60GHz 16 GB            1 GB                  900 GB              Windows 7 Professionnel
User20_PCW7     Intel(R) Core(TM) i3-5900K CPU @ 2.60GHz 6 GB             500 MB                200 GB              Windows 7 Professionnel
User21_PCW10    Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User22_PCW7     Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB             200 MB                250 GB              Microsoft Windows 7 Professionnel
User23_PCW10    Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB            2 GB                  1500 GB             Windows 10 ?ducation
User24_PCW95    Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB             100 MB                120 GB              Windows 95
User25_PCW7     Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB            1 GB                  900 GB              Windows 7 Professionnel
User26_PCW10    Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User27_PCWVista Intel(R) Core(TM) i5-1900K CPU @ 1.60GHz 1 GB             50 MB                 120 GB              Windows Vista Professionel
User28_PCW10    Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation
User29_PCW7     Intel(R) Core(TM) i5-8900K CPU @ 3.60GHz 16 GB            1 GB                  900 GB              Windows 7 Professionnel
User30_PCW7     Intel(R) Core(TM) i3-5900K CPU @ 2.60GHz 6 GB             500 MB                200 GB              Windows 7 Professionnel

powershell 控制台上的第二个 csv :

2nd Csv https://i.stack.imgur.com/Qmfvx.png

PS Z:\Partage> $PrestaCSV | Format-Table

PC           Nom_du_prestataire Date_de_Migration
--           ------------------ -----------------
PCW          esgi-src12\presta1 17_07_21_01:35
PWX          esgi-src12\presta1 17_07_21_01:36
User10_PCW7  esgi-src12\presta1 17_07_21_01:50
User6_PCW10  esgi-src12\presta1 17_07_21_01:55
User6_PCW10  esgi-src12\presta1 17_07_21_01:56
User6_PCW10  esgi-src12\presta1 17_07_21_02:01
User11_PCW10 esgi-src12\presta1 17_07_21_02:01
User25_PCW7  esgi-src12\presta2 17_07_21_02:07

我想做的是: 1 - 在第一个 csv 中添加最后两列 2 - 仅更新第一个 csv 中的相关行

像那样 :示例数组 https://i.stack.imgur.com/oBe4x.png

1st CSV :            2nd csv:                 Result:
| A | B | C |       | A | Y | X |            | A | B | C | Y | X |
|---|---|---|       |---|---|---|            |---|---|---|---|---|
| A1| B1| C1|       | A2| Y2| X2|----------\ | A1| B1| C1|   |   |   
| A2| B2| C2|   +   | A3| Y3| X3|----------/ | A2| B2| C2| Y2| X2| 
| A3| B3| C3|       |   |   |   |            | A3| B3| C3| Y3| X3|

这是我尝试执行的脚本:

# TEST 4 => Crée les deux colonnes supplémentaires mais ne met pas les bonnes données
$NomCsvInitiale = "\\ADSERVER\Inventaire\InventairePCAD.csv"
$CSVInitiale = Import-Csv -Path '\\ADSERVER\Inventaire\InventairePCAD.csv' -Delimiter ','
$Folder = "\\ADSERVER\Inventaire\"
$GetAllCsv = (Get-ChildItem -Path $Folder -Filter 'Presta*.csv').FullName
$PrestaCSV = Import-Csv -Path $GetAllCsv -Delimiter ','

foreach ( $PrestaCSV1 in $PrestaCSV) {
    if ($CSVInitiale.PC -eq $PrestaCSV1.PC) {
        $CSVInitiale | Select-Object *, @{n="Nom_du_prestataire";e={$PrestaCSV1.Nom_du_prestataire}}, @{n="Date_de_Migration";e={$PrestaCSV1.Date_de_Migration}} | Export-Csv -Path $NomCsvInitiale -NoTypeInformation;
        } elseif ($CSVInitiale.PC -ne $PrestaCSV1.PC) {
        $CSVInitiale | Select-Object *, @{n="Nom_du_prestataire";e={" "}}, @{n="Date_de_Migration";e={" "}} | Export-Csv -Path $NomCsvInitiale -NoTypeInformation;
}
}

它确实添加了两列,但只将最后一行放在所有 PC 上。

实际上结果示例:

PS Z:\Partage> $PrestaCSV

PC           Nom_du_prestataire Date_de_Migration
--           ------------------ -----------------
PCW          esgi-src12\presta1 17_07_21_01:35
PWX          esgi-src12\presta1 17_07_21_01:36
User10_PCW7  esgi-src12\presta1 17_07_21_01:50
User6_PCW10  esgi-src12\presta1 17_07_21_01:55
User6_PCW10  esgi-src12\presta1 17_07_21_01:56
User6_PCW10  esgi-src12\presta1 17_07_21_02:01
User11_PCW10 esgi-src12\presta1 17_07_21_02:01
User25_PCW7  esgi-src12\presta2 17_07_21_02:07


PS Z:\Partage> $CSVInitiale | Format-Table

PC              Processeur                               Taille_de_la_RAM Capacite_carte_reseau Taille_du_dique_dur Systeme_d_exploitation                   Nom_du_prestataire Date_de_Migration
--              ----------                               ---------------- --------------------- ------------------- ----------------------                   ------------------ -----------------
ADSERVER        Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB             1 GB                  60,00 GB            Microsoft Windows Server 2016 Datacenter esgi-src12\presta2 17_07_21_02:07
PCW10           Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB             1 GB                  60,00 GB            Microsoft Windows 10 ?ducation           esgi-src12\presta2 17_07_21_02:07
PCW7            Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 1 GB             1 GB                  80,00 GB            Microsoft Windows 7 Professionnel        esgi-src12\presta2 17_07_21_02:07
User1_PCW10     Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation                     esgi-src12\presta2 17_07_21_02:07
User2_PCW7      Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB             200 MB                250 GB              Microsoft Windows 7 Professionnel        esgi-src12\presta2 17_07_21_02:07
User3_PCW10     Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB            2 GB                  1500 GB             Windows 10 ?ducation                     esgi-src12\presta2 17_07_21_02:07
User4_PCW95     Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB             100 MB                120 GB              Windows 95                               esgi-src12\presta2 17_07_21_02:07
User5_PCW7      Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB            1 GB                  900 GB              Windows 7 Professionnel                  esgi-src12\presta2 17_07_21_02:07
User6_PCW10     Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB            420 MB                900 GB              Windows 10 ?ducation                     esgi-src12\presta2 17_07_21_02:07

阅读论坛,我想我明白了不可能直接收集它们,有必要重新创建一个csv文件,对吗?这对我来说不再适用了:(

Regards.


您可以根据中的匹配值将第二个 csv 中的列添加到第一个 csv 文件中PC列如下:

$NomCsvInitiale = '\\ADSERVER\Inventaire\InventairePCAD.csv'
$NomCsvPresta   = '\\ADSERVER\Inventaire\Presta.csv'

# import both csv files
$inventory = Import-Csv -Path $NomCsvInitiale -Encoding UTF8
$addendum  = Import-Csv -Path $NomCsvPresta -Encoding UTF8

# loop over the items in the $inventory data
$result = foreach ($item in $inventory) {
    # try and find a matching item in the $addendum, based op the PC column
    $matchingItem = $addendum | Where-Object { $_.PC -eq $item.PC }
    # output a new object with the two columns added
    $item | Select-Object *, 
                          @{Name = 'Nom_du_prestataire'; Expression = {$matchingItem.Nom_du_prestataire}},
                          @{Name = 'Date_de_Migration'; Expression =  {$matchingItem.Date_de_Migration}}
}

# save the captured new data to file
$result | Export-Csv -Path $NomCsvInitiale -NoTypeInformation -Encoding UTF8
  • Import-Csv 默认使用逗号作为分隔符,因此在这种情况下您不必指定
  • 我注意到,对于您的数据,您需要添加参数-Encoding UTF8,为了正确处理数据:Microsoft Windows 10 ?ducation --> Microsoft Windows 10 Éducation
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POWERSHELL - 添加列 + 更新几行 的相关文章

随机推荐

  • 如何让flutter应用支持电视设备?

    电视设备不支持触摸操作 用户必须使用遥控器 我们需要通过按左 上 右 下方向键来移动焦点 当目标部件获得焦点时 我们将按确定按钮来响应各种按键事件 但我找不到任何flutter接口来解决这种交互 有人可以帮助我吗 有用于此目的的 Syste
  • 将密码保存在“帐户和密码”下的首选项中

    苹果在 WWDC 上展示了 iOS 应用程序的自动填充功能推介会 https developer apple com videos play wwdc2017 206 它的设置相当简单 而且效果非常好 如果用户在safari中输入密码并保存
  • Android java.util.concurrent.TimeUnit 将毫秒转换为分钟

    有没有办法使用 java util concurrent TimeUnit 将毫秒转换为分钟 这个答案 https stackoverflow com questions 625433 how to convert milliseconds
  • 有没有办法将 Google 地图标记始终固定在其地图的中心?

    更准确地说 我试图完成的是 当我Drag谷歌地图 有谷歌地图Marker 始终固定在地图的中心 我认为这将改善用户体验 这就是我现在正在做的 var map marker options options center new google
  • Android Studio 设计时数据绑定回退/默认值

    我正在使用 Android 数据绑定 效果很好
  • JAXB 覆盖列表的 @XmlElement 类型

    有一个简单的类Bean1带有类型的子列表BeanChild1 XmlRootElement name bean XmlAccessorType XmlAccessType PROPERTY public static class Bean1
  • 如何以特定格式对数组中的奇数和偶数进行排序

    我有一个像这样的向量 seq vector lt c 3 12 5 9 11 8 4 6 7 11 15 3 9 10 12 2 我想按奇数降序排列它们 然后按偶数升序排列它们 上述 seq vector 的输出将是 new seq vec
  • 使用 Laravel 迁移将默认值设置为 NULL

    我正在迁移中的表中添加一个字段 我希望允许该字段为 NULL 但我也希望它默认为 NULL 我在默认方法中放置什么 我担心把 NULL in 将尝试放置一串NULL我显然不想要 请帮忙 Schema table item categorie
  • 无法更改复合类型,因为列正在使用它

    我正在尝试更改表中的列当前正在使用的复合类型的属性 我正在使用命令 ALTER TYPE typeName ALTER ATTRIBUTE attributeName SET DATA TYPE text 不幸的是 我收到错误 ERROR
  • 在项目中使用 gradle 插件,其依赖项也使用相同的插件

    我是 gradle 新手 从 Maven 离开 现在我有一个问题 我有一个 gradle 构建 我想在其中使用com bmuschko nexus插入 但我的项目依赖于另一个项目 我也想在其中使用com bmuschko nexus plu
  • Bonobo Git 服务器 - HTTP 错误 500.19 - 代码:0x80070021

    安装后访问系统时出现此错误 操作系统 Windows 2012 R2 我遇到了同样的问题 我相信 IIS 不知何故丢失了 MVC 框架的注册 通过以下方式重做注册 Windows 8 Windows Server 2012 Windows
  • 低功耗蓝牙 Android - 在后台搜索

    我想知道是否可以在后台检查是否有人进入或离开信标区域 例如 在 iOS 中 您可以使用以下方法didEnterRegion or didExitRegion从后台发送通知 是否有任何本地可能性或有人有解决方法 Android根本没有任何 原
  • 从 javascript 堆栈跟踪中查找 Java 实际堆栈跟踪

    我们实现了一种从 GWT 客户端记录服务器异常的通用机制 当然 其中一些是意外的异常 例如 NPE 因此我们在日志中得到了这些堆栈跟踪 摘录 java lang Throwable One or more exceptions caught
  • 使用 Android 的新指纹 API 供多个用户使用

    我们正在 Android 上创建一个时间戳应用程序 理想情况下这将使用指纹来识别用户 在一个组织中可能有大约 150 个不同的用户 指纹 API 是否允许多个用户 或者目前仅适用于设备所有者 如果这只适用于当前用户 是否有其他可用的 API
  • Jinja2 过滤器将自定义标记转换为 html

    拥有自动转义属性 我想保持这种方式 我希望用户能够输入一些自定义标记 以便有机会格式化文本 例如 s s 将被翻译成 strong strong 我相信正确的方法是编写自定义 Jinja2 过滤器 但以下方法不起作用 app templat
  • 异步实际上是如何工作的......?

    我一直在研究大量的多线程 回调 调度队列 同步和异步方式 我研究得越多 得到的就越多困惑和沮丧以至于我觉得我似乎永远无法理解它 请有人能引导我朝着正确的方向开始 到目前为止我发现的大部分信息都是关于什么是做什么和优势的东西 我什么真正想知道
  • 将文本字符串解析为 F# 代码

    如何获取应该是 F 代码的文本字符串 并将其解析为 F 代码 以在屏幕上打印结果 我猜测这个问题可以通过 NET 中的一个功能来解决 因此可以通过 F 本身或 C 来完成 这可能以什么方式解决tryfsharp org http www t
  • Numpy 多维子集

    我花了很长时间努力寻找这个问题的答案 但没有找到任何完全符合要求的答案 我有一个多维 numpy 数组 其中包含数据 在我的例子中是 3 维 和另一个数组 2 维 其中包含有关我想要沿原始数组的最后一个维度的值的信息 例如 这是一个说明问题
  • 将路径保存在字符串中

    private static final String BasePath home rafael pesquisa ImageSimilarity 我正在开发一个图像比较程序 我从某处找到了这行代码 然而 在上述情况下 路径是为 Linux
  • POWERSHELL - 添加列 + 更新几行

    我是 PowerShell 新手 我目前正在尝试合并两个不同的 csv 一个有 7 列和 31 行 另一个有3列 大约10行 他们只有 PC 列是共同的 其余的我必须添加列 我尝试过几种操作 但没有成功 这是 powershell 控制台上