[英]How to remove remaining text in each line in an array after first instance of special character in PowerShell
I would like to remove the remaining text in each line of an array, including and after the first instance of a period character (ie '.'). 我想删除数组每行中的剩余文本,包括和在句点字符的第一个实例之后(即'。')。 This is the code I'm using but I'm getting this error: Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero. Parameter name: length"
这是我正在使用的代码,但是我收到了这个错误:异常调用带有“2”参数的“Substring”:“Length不能小于零。参数名称:length”
$Textfile = Get-Content "C:\Temp\Text.txt"
$Textfile | Foreach {
$Textfile.Substring(1,$Textfile.IndexOf('.'))
}
$Textfile | Out-File C:\Temp\Text2.txt
Try something like this: 尝试这样的事情:
Get-Content myfile.txt | foreach {@($_ -split '\.')[0]}
Notes: 笔记:
Inside your Foreach loop, you need to work on the current item, $_
instead of the entire $Textfile array. 在Foreach循环中,您需要处理当前项目
$_
而不是整个$ Textfile数组。
Should probably be something like this: 应该是这样的:
$Textfile | Foreach {
$_.Substring(1,$Textfile.IndexOf('.'))
} | Out-File C:\Temp\Text2.txt
Please note that you may still get the same error if it comes across a line without a '.' 请注意,如果遇到没有“。”的行,您仍可能会收到同样的错误。
Try the next approach: 尝试下一个方法:
$Textfile|Foreach {$_ -replace '(^[^.]*)(.*)','$1'}|Out-File C:\Temp\Text2.txt
... or other: ... 或其他:
$Textfile|
Foreach {if(($p=$_.indexof('.')) -ge 0){$_.substring(0,$p)}else{$_}}|
Out-File C:\Temp\Text2.txt
The problem I see is that your $TextFile variable holds the entire contents of the file, but you call substring on the index of period in the entire contents of the file. 我看到的问题是你的$ TextFile变量保存文件的全部内容,但是你在文件的整个内容中的句点索引上调用substring。 Also, you call substring from 1, not 0. So after the second call, you'll be guaranteed to receive that error.
此外,您从1调用子字符串,而不是0.因此在第二次调用后,您将保证收到该错误。 My attempts to correct it below.
我试图在下面纠正它。
$Textfile = Get-Content "C:\Temp\Text.txt"
$NewFileContents = @($Textfile | Foreach {
if ($_.IndexOf(".") -eq -1)
{
$_
}
else
{
$_.Substring(0,$_.IndexOf('.'))
}
}) -Join "`r`n"
$NewFileContents | Out-File C:\Temp\Text2.txt
More code explanation: String indexing begins at 0, not 1. In other words, the first character is 0. When you called $FileContents.Substring(1), you were eliminating the first character each time. 更多代码说明:字符串索引从0开始,而不是1.换句话说,第一个字符是0.当你调用$ FileContents.Substring(1)时,你每次都删除第一个字符。 Since you also called it on the variable $FileContents each time, and not on the line of the new file, it consistently got shorter.
由于您每次都在变量$ FileContents上调用它,而不是在新文件的行上调用它,因此它一直变短。 Using the anonymous variable that comes from the foreach loop ($_), you get the contents of each line.
使用来自foreach循环($ _)的匿名变量,可以获得每行的内容。
I call ($ .IndexOf(".") -eq -1) to verify if there exists a period on the line. 我调用($ .IndexOf(“。”) - eq -1)来验证该行上是否存在句点。 By checking if there exists a period on each line before doing the substring, we remove the possibility of saying $ .Substring(0,-1) which would give unpredictable results.
通过在执行子字符串之前检查每行上是否存在句点,我们删除了说$ .Substring(0,-1) 的可能性,这将产生不可预测的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.