Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershellv2-从字符串中删除最后x个字符_Powershell_Substring_Powershell 2.0 - Fatal编程技术网

Powershellv2-从字符串中删除最后x个字符

Powershellv2-从字符串中删除最后x个字符,powershell,substring,powershell-2.0,Powershell,Substring,Powershell 2.0,我有一个包含几千行文本的文件。我需要从中提取一些数据, 但我需要的数据总是从左起57个字符,从右起37个字符。 我需要的钻头(中间)长度不一 e、 g.20141126\u这段文字需要删除这段文字需要删除这段文字也需要删除这段文字 到目前为止,我得到了: SELECT-STRING -path path_to_logfile.log -pattern "20141126.*<b>" | FOREACH{$_.Line} | FOREACH{ $_.substring(57

我有一个包含几千行文本的文件。我需要从中提取一些数据, 但我需要的数据总是从左起57个字符,从右起37个字符。 我需要的钻头(中间)长度不一

e、 g.
20141126\u这段文字需要删除这段文字需要删除这段文字也需要删除这段文字

到目前为止,我得到了:

SELECT-STRING -path path_to_logfile.log -pattern "20141126.*<b>" | 
FOREACH{$_.Line} | 
FOREACH{
    $_.substring(57)
    }
但这些都不起作用


有没有办法去掉最后的x个字符?

这是如何从字符串中删除最后的37个字符:

$_.subString(0,$_.length-37)

但是arco的答案是解决您整体问题的首选方案

如果我理解正确,您需要:

$_.substring(57,$_.length-57-37)

虽然这似乎与您给出的示例不完全一致,但它将提供不同的中间部分,即从开始57个字符开始,从结束37个字符结束

要删除文本中的最后x个字符,请使用:

$text -replace ".{x}$"


为此干杯,简单但有效,正是我喜欢的方式。感谢您是一个与powershell相关的trim命令,可用于非字符串值。
$text -replace ".{x}$"
PS>$text= "this is a number 1234"
PS>$text -replace ".{5}$" #drop last 5 chars
this is a number