如何搜索文本文件中的第一行和最后一行?

Bli*_*ank 5 powershell powershell-ise

我只需要在文本文件中搜索第一行和最后一行来查找" - "并将其删除.我该怎么做?我尝试了select-string,但我不知道找到第1行和最后一行,只从那里删除" - ".

这是文本文件的样子:

 % 01-A247M15 G70 
N0001 G30 G17 X-100 Y-100 Z0
N0002 G31 G90 X100 Y100 Z45
N0003 ; --PART NO.:  NC-HON.PHX01.COVER-SHOE.DET-1000.050 
N0004 ; --TOOL:  8.55 X .3937 
N0005 ;  
N0006  % 01-A247M15 G70 
Run Code Online (Sandbox Code Playgroud)

像这样的东西?

$1 = Get-Content C:\work\test\01.I

$1 | select-object -index 0, ($1.count-1)
Run Code Online (Sandbox Code Playgroud)

CB.*_*CB. 6

尝试:

$txt = get-content c:\myfile.txt
$txt[0] = $txt[0] -replace '-'
$txt[$txt.length - 1 ] = $txt[$txt.length - 1 ] -replace '-'
$txt | set-content c:\myfile.txt
Run Code Online (Sandbox Code Playgroud)

  • 拼写错误:$ a - > $ txt(最后一行).$ txt [1] - > $ txt [0] :-) (2认同)
  • 此外,您可以调用`$ txt [-1]`来获取数组中的最后一项 (2认同)

Nic*_*ick 6

好吧,所以在看了一会儿之后,我决定必须有一种方法来做一个班轮.这里是:

(gc "c:\myfile.txt") | % -Begin {$test = (gc "c:\myfile.txt" | select -first 1 -last 1)} -Process {if ( $_ -eq $test[0] -or $_ -eq $test[-1] ) { $_ -replace "-" } else { $_ }} | Set-Content "c:\myfile.txt"
Run Code Online (Sandbox Code Playgroud)

以下是对此操作的细分:

首先,那些现在熟悉的别名.我只是将它们放入,因为命令足够长,所以这有助于保持可管理性:

  1. gc 手段 Get-Content
  2. % 手段 Foreach
  3. $_ 是当前管道值(这不是别名,但我想我会定义它,因为你说你是新的)

好的,现在这里发生了什么:

  1. (gc "c:\myfile.txt") | - >获取内容c:\myfile.txt并将其发送到该行
  2. % - > foreach循环(单独遍历管道中的每个项目)
  3. -Begin {$test = (gc "c:\myfile.txt" | select -first 1 -last 1)} - >这是一个开始块,它在进入管道之前运行所有内容.它将第一行和最后一行加载c:\myfile.txt到一个数组中,以便我们检查第一个和最后一个项目
  4. -Process {if ( $_ -eq $test[0] -or $_ -eq $test[-1] ) - >这会检查管道中的每个项目,检查它是文件中的第一个项目还是最后一个项目
  5. { $_ -replace "-" } else { $_ } - >如果它是第一个或最后一个,它会进行替换,如果不是,它只是不管它
  6. | Set-Content "c:\myfile.txt" - >这会将新值放回文件中.

有关以下各项的详细信息,请参阅以下网站:

Get-Content使用
Get-Content定义
Foreach Foreach
的Pipeline
Begin和Process部分(这通常用于自定义函数,但它们也在foreach循环中工作)
If ... else语句
Set-Content

所以我在考虑如果你想对许多文件执行此操作,或者想要经常这样做.我决定制作一个能满足你要求的功能.这是功能:

function Replace-FirstLast {
    [CmdletBinding()]
    param(
        [Parameter( `
            Position=0, `
            Mandatory=$true)]
        [String]$File,
        [Parameter( `
            Position=1, `
            Mandatory=$true)]
        [ValidateNotNull()]
        [regex]$Regex,
        [Parameter( `
            position=2, `
            Mandatory=$false)]
        [string]$ReplaceWith=""
    )

Begin {
    $lines = Get-Content $File
} #end begin 

Process {
    foreach ($line in $lines) {
        if ( $line -eq $lines[0]  ) {
            $lines[0] = $line -replace $Regex,$ReplaceWith 
        } #end if
        if ( $line -eq $lines[-1] ) {
            $lines[-1] = $line -replace $Regex,$ReplaceWith
        }
    } #end foreach
}#End process

end {
    $lines | Set-Content $File
}#end end

} #end function
Run Code Online (Sandbox Code Playgroud)

这将创建一个名为的命令Replace-FirstLast.会像这样调用:

Replace-FirstLast -File "C:\myfiles.txt" -Regex "-" -ReplaceWith "NewText"
Run Code Online (Sandbox Code Playgroud)

-Replacewith是可选的,如果它是空白,它将只删除(默认值"").在-Regex正在寻找一个正则表达式匹配您的命令.有关将此信息放入您的配置文件的信息,请查看本文

请注意: 如果您的文件非常大(几GB),这不是最好的解决方案.这会导致整个文件存在于内存中,这可能会导致其他问题.