【发布时间】:2021-03-21 02:51:54
【问题描述】:
尝试学习如何使用 Powershell 来帮助从各种来源提取数据并将我的头撞到格式不正确的 CSV 上。我有一个 CSV,它在每行的第一个单元格中只有一个文本字符串。每行都有一个文件的哈希值、一些数据和编写它的程序的哈希值。我需要将下面“link hash=”之后的第一个哈希提取到另一个文件中。
[enter code here][1]
如果图像不起作用,请阅读第一行:
The file "<share><link hash="randomhash1">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash2">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash3">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash4">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
我一直在寻找一种方法来告诉 powershell 在新的 CSV 中为我提供 "hash=" "" 的值,但是遇到了语法错误并且显然这样做是非常错误的。当我试图学习这一点时,如果有人能说这是正则表达式,我会永远感激不尽。
以下是我尝试过的。查看 Select-String 我对如何让正则表达式匹配成为我可以输出的对象有一个知识差距。如果我取出 ForEach-Object 参数,我会得到一个 csv,其中每一行都有一个真实的陈述。
导入-Csv C:\Users\me\Desktop\Bad_hash.csv |选择字符串-模式 "(?\w+)" | ForEach-Object {$hash = $_.Matches[0].Groups['hash'].Value [PSCustomObject] @{ Hash = $hash}} | |导出-Csv C:\Users\me\Desktop\test.csv
【问题讨论】:
-
您没有包含任何代码或示例数据。请使用示例 CSV 数据以及您正在使用的脚本/代码以及您期望的输出来更新您的问题。
-
您的更新仅包含您要查找的文本,这很好,但我们确实需要 CSV 文件的样本(当然,缩短为几行并进行了适当的编辑)。您还没有发布您用来尝试解析它的脚本/代码。
-
@Bill_Stewart - 谢谢,我一直在戳 Select-String 看看我是否能解决这个问题。学习 PowerShell 仅一个月左右。我已经投入了我一直在做的事情。
-
您仍然没有包含 CSV 样本。
标签: powershell csv