在Powershell中,从左到右抓取文件中每行的前10个字符

Noe*_*sis 5 powershell

我想从$ line中获取前x个字符并将它们移动到变量中.

我该怎么做呢?

这是我现有的代码

$data = get-content "C:\TestFile.txt"
foreach($line in $data)
{
   if($line.length -gt 250){**get first x amount of characters into variable Y**}
}
Run Code Online (Sandbox Code Playgroud)

CB.*_*CB. 7

像这样:

$data = get-content "TestFile.txt"
$amount = 10;
$y= @()
foreach($line in $data)
{
   if ( $line.length -gt 250){ $y += $line.substring(0,$amount) } 
}
Run Code Online (Sandbox Code Playgroud)

  • 或者`$ line [-11 ..- 1] -join''`如果你想变得超酷;-) (2认同)