Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 输出的文本提取和重新格式化_Powershell - Fatal编程技术网

Powershell 输出的文本提取和重新格式化

Powershell 输出的文本提取和重新格式化,powershell,Powershell,我有一个文本文件e:\abc.txt,其中包含此格式的数据 Heading Asia Name china val 200 drt fun and play END Heading Europe Name paris val 234234 drt tour END Heading america Name Ny val 234 drt shop END [continued like this..] 我想创建一个新的文本文件xyz.txt,它以以下格式提取选定的标题和d

我有一个文本文件e:\abc.txt,其中包含此格式的数据

Heading Asia
 Name china
 val 200
 drt fun and play
END

Heading Europe
 Name paris
 val 234234
 drt tour
END

Heading america
 Name Ny
 val 234
 drt shop
END

[continued like this..]
我想创建一个新的文本文件xyz.txt,它以以下格式提取选定的标题和drt

Heading    drt
asia       fun and play
Europe     tour
我尝试了以下脚本:

$source e:\abc.txt $search亚洲、欧洲 $a=选择字符串$source-模式$search-上下文0,3 输出

Heading Asia
Name china
val 200
drt fun and play
Heading Europe
Name paris
val 234234
如何在行间删除

Heading   - drt
asia      - fun and play
Europe    - tour

因为你至少已经尝试过一些东西了,所以把这个提出来帮助你。我希望这不会超出您的理解,但它使用正则表达式帮助将文件分解为各个部分,然后将每个部分转换为自己的对象。这样做的好处是,我们可以使用命令PowerShell cmdlet(如Where对象)进行筛选。这并不像我希望的那样简洁,但正如我所说的,我只是想做一些更容易理解的事情

$path=e:\abc.txt $headingstoMatch=亚洲、欧洲 获取内容$path | Out String-split END | Where Object{$\匹配\w}| ForEach Object{ $hash=$\.Trim-split`r`n-replace^\s+-replace^.*\s,$1='| Out String |从StringData转换 新对象-TypeName PSCustomObject-属性$hash }|其中对象{$headingstoMatch-包含$\ Heading}|选择对象Heading,drt 使用上面的文本文件,您将得到如下内容

Heading drt         
------- ---         
Asia    fun and play
Europe  tour  

现在,您可以使用导出CSV之类的工具轻松地将其导出到文件中。

您尝试了什么?我也不知道你想做什么-1你为什么要重复发帖?你一小时前已经问过这个问题了。。。这些问题没有表现出任何努力、试图理解或演示如何解决您面临的问题。另外,有人已经回答了上一个问题。可能重复的请理解,我们想帮助您,但您需要回答我们的问题。我厌倦了这么多脚本。但不起作用Anks@matt它起作用了。事实上,我对powershell是新手。我尽力了。没有给出正确的代码是我的错误