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

标签 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**}
}

最佳答案

像这样:

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

关于powershell - 在Powershell中从左到右抓取文件中每行的前10个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12534569/

相关文章:

java - 我想以管理员身份运行 PowerShell 并通过 Java 在管理员电源 shell 中执行命令

winforms - 将 argb 转换为字符串

powershell - 如何使用Powershell转换文件内容

loops - 如何在用户名末尾添加数字

powershell - 复制前检查源文件是否存在

powershell - 尽管有正在运行的服务,Docker 容器仍会退出

powershell - 使用基于环境变量的路径运行脚本

powershell - $PSScriptRoot 和 $MyInvocation 之间的功能差异

powershell - 从 robocopy 日志中删除百分比

powershell - 将带有转义符的参数从PowerShell提示符传递到批处理文件