![](/img/trans.png)
[英]PowerShell: Read text, regex sort, write output to file and formatting
[英]Powershell - Read a single text file and sort contents to multiple files based on text within the line
我正在寻找有关如何逐行读取文件,然后将基于搜索条件的行复制到新创建的文件的方向。 由于我的描述可能很差,因此我尝试说明以下内容:
Single Text File Sample:
Name=N0060093G
Name=N0060093H
Name=N400205PW
Name=N400205PX
Name=N966O85Q0
Name=N966O85Q1
该脚本将读取每一行,并在"Name=N"
"###"
之后使用"###"
,以在标识符"###"
之后创建一个新文件名,以将每行适当的内容复制到新文件中。 因此,行"Name=N0060093G"
和"Name=N0060093H"
将转到“ 006 .txt”; "Name=N400205PW"
和"Name=N400205PX"
将写入“ 400 .txt”, "Name=N400205PX"
。
RegEx样式方法:
$File = 'test.txt'
Get-Content $File | ForEach {
If ($_ -match '^Name\=N(?<filename>\d{3}).*') {
$_ | Out-File -Append "$($Matches.Filename).txt" -WhatIf
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.