简体   繁体   English

如何在PowerShell中的第一个特殊字符实例后删除数组中每行中的剩余文本

[英]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: 笔记:

  1. The foreach processes the file one line at a time (but you knew that). foreach一次处理一行文件(但你知道)。
  2. The @ forces the result to be an array, even if there is only one string, in case no dots. @强制结果为数组,即使只有一个字符串,如果没有点。
  3. The $_ references the current line of the file. $ _引用文件的当前行。
  4. The backslash before the dot is an escape character in Regex 点之前的反斜杠是Regex中的转义字符
  5. The [0] selects the first item from the array. [0]选择数组中的第一项。

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.

相关问题 Powershell在第一个特殊字符实例后删除文本 - Powershell remove text after first instance of special character 如何删除 Windows PowerShell 中文本文件中每个(相同)行的 1 个实例并将剩余的相同行分组? - How to remove 1 instance of each (identical) line in a text file in Windows PowerShell and group the remaining identical lines? PowerShell删除URL列表中特殊字符后的文本 - PowerShell to remove text after a special character in URL list powershell:如何删除每行文本中的第一个#? - powershell: how to remove first # in each row of text? 在POWERSHELL中运行脚本后,从文本文件中删除第一行 - REMOVE FIRST LINE from TEXT file AFTER running a script in POWERSHELL Powershell:如何在特殊字符后获取单词 - Powershell: How to fetch word after special character Powershell:如何删除数组的控制台输出中的第一个空空白行? - Powershell: How to remove first empty blank line in console output of an array? PowerShell-txt文件中特殊字符后的小写第一个单词 - PowerShell - lowercase first word after special character in txt files 数组未按预期存储文本文件中的数据。 如何使用Powershell将每行的第一个单词放入数组? - Array is not storing data from text file as expected. How to get first word of each line into array using Powershell? 如何使用 powershell 脚本在文本文件的行首和行尾添加特殊字符? - How to add special character at the beginning and end of a line in a text file using a powershell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM