Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
Powershell 从字符串行中删除空格并在数组中使用_Powershell_Powershell 2.0 - Fatal编程技术网

Powershell 从字符串行中删除空格并在数组中使用

Powershell 从字符串行中删除空格并在数组中使用,powershell,powershell-2.0,Powershell,Powershell 2.0,我在浏览以前的文章,但我找不到非常具体的答案,如何替换字符串中的空白,然后将其用作数组 输入文件由以下行组成: -rw-r--r-- 1 myuser admin 315279199 May 12 02:46 2016_05_12_backup.tar.gz -rw-r--r-- 1 myuser admin 315278122 May 13 04:56 2016_05_13_backup.tar.gz 我希望接收以下输出: program executed on 2016-0

我在浏览以前的文章,但我找不到非常具体的答案,如何替换字符串中的空白,然后将其用作数组

输入文件由以下行组成:

-rw-r--r--   1 myuser admin   315279199 May 12 02:46 2016_05_12_backup.tar.gz
-rw-r--r--   1 myuser admin   315278122 May 13 04:56 2016_05_13_backup.tar.gz
我希望接收以下输出:

program executed on 2016-05-16 / 12:18:06
to unix: 
rm -fr 2016_05_12_backup.tar.gz
rm -fr 2016_05_13_backup.tar.gz


to excel log: 
2016_05_12_backup.tar.gz
2016_05_13_backup.tar.gz

=============== END  ==============
我的代码在这里:

$path_in = "C:\test\input.txt"
$path_out = "C:\test\output.txt"

$endMessage = "=============== END  =============="

$reader = [System.IO.File]::OpenText($path_in)
$get_time_message = "program executed on " + [datetime]::now.ToString('yyyy-MM-dd / HH:mm:ss')

try {

add-content $path_out $get_time_message
add-content $path_out "to unix: "

$long_string_to_excel =""


    while($true){
        $line = $reader.ReadLine()

        if ($line -eq $null) { break }

        # divide the input line into array - remove white space 
        # it is hard coded here below for the lines that consist two and three space characters

        $better_line =  $line.replace('   ',' ')
        $best_line = $better_line.replace('  ',' ').split(' ')

        $stringToOutput = "rm -fr " + $best_line[8]

        $long_string_to_excel = $long_string_to_excel + $best_line[8]  + "`r`n"

        add-content $path_out $stringToOutput

    }

    add-content $path_out "`n"
    add-content $path_out "to excel log: "
    add-content $path_out $long_string_to_excel
    add-content $path_out $endMessage

}
finally {
    $reader.Close()
}
write-host "program execution:`ncompleted"
这个脚本工作正常,但它是为包含两个和三个空格字符的输入行“硬”编码的。我想用

    $better_line =  $line.replace(' +', ' ');
    $best_line = $better_line.split(' ')
而不是

    $better_line =  $line.replace('   ',' ')
    $best_line = $better_line.replace('  ',' ').split(' ')
但结果是不正确的:

program executed on 2016-05-16 / 12:18:04
to unix: 
rm -fr 315279199
rm -fr 315278122


to excel log: 
315279199
315278122

=============== END  ==============

您能否就如何替换硬编码部分的解决方案提供建议,以使脚本适用于单行中的任何类型的空格?

请使用内置的方法,而不是静态的
String.Split()
方法,它支持正则表达式,以便您可以使用它按“1个或多个空格”进行拆分,例如:

PS C:\> "a   b" -split '\s+'
a
b
PS C:\> "a b" -split '\s+'
a
b

如何从PowerShell中的阵列中删除空项:

直观的方法:检查数组中的每个项目并验证其是否为空

    $best_line = $line.split(' ') | ? {$_}
散漫的(.NET)方法:

类型:
System.StringSplitOptions

  • StringSplitOptions.RemoveEmptyEntries
    从返回的数组中省略空数组元素;或
  • StringSplitOptions.None
    在返回的数组中包含空数组元素

谢谢你的回答。我将检查System.StringSplitOptions。
    $best_line = $line.split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)