Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
用“替换缺少的输入数据”;0“;在带有Powershell的输出csv/txt中_Powershell_Csv_Replace - Fatal编程技术网

用“替换缺少的输入数据”;0“;在带有Powershell的输出csv/txt中

用“替换缺少的输入数据”;0“;在带有Powershell的输出csv/txt中,powershell,csv,replace,Powershell,Csv,Replace,我有一个包含以下数百行(对象)的GIS输入文件: 位置表示长度、宽度、高度[x、y、z]。目前,我使用此powershell脚本转换下一个工具的输入: $d=gc input.txt $rows=@() for ($i=0; $i -le $d.count -2; ++$i) { if( $d[$i] -match "POSITION" ) { $pos = $d[$i].Replace('POS

我有一个包含以下数百行(对象)的GIS输入文件:

位置表示长度、宽度、高度[x、y、z]。目前,我使用此powershell脚本转换下一个工具的输入:

$d=gc input.txt   
$rows=@()   
for ($i=0; $i -le $d.count -2; ++$i) {                                          
if( $d[$i] -match "POSITION" ) { 
 $pos = $d[$i].Replace('POSITION="[', '').Replace(',',';').Replace(']";','').Replace(' ', '').trim()  
 $typ = $d[$i+1].Replace('TYPE=', '').Replace(';', '').Replace(' ', '').trim() 
 $ori = $d[$i+2].Replace('AZIMUT="', '').Replace('"', '').Replace('PARENT=;', '').Replace(' ', '').trim() 
 $rows += $("{0};{1};{2}" -f $typ,$pos,$ori)            
} 
}

sc -path output.csv -value $rows
output.txt如下所示:

"Saltlake";950.28174;797.89899;-25.722504;
"Oak";900.71307;777.2226;-3.8146973e-006;-168.15758;
 "Saltlake";950.28174;797.89899;0;-25.722504;
 "Oak";900.71307;777.2226;-3.8146973e-006;-168.15758;
发生的情况是,下一个转换工具错误地将“Saltlake”的方位角值作为其Z位置,因为在input.txt位置的y值之后没有给出输入,这与“Oak”不同

现在,是否可以用“0”替换缺少的Z输入空间,当然,如果给定了Z位置,就不要替换它?

其目的是使output.txt如下所示:

"Saltlake";950.28174;797.89899;-25.722504;
"Oak";900.71307;777.2226;-3.8146973e-006;-168.15758;
 "Saltlake";950.28174;797.89899;0;-25.722504;
 "Oak";900.71307;777.2226;-3.8146973e-006;-168.15758;

我加了一行,使这项工作。我使用
-split
操作符检查该位置有多少零件(通过在分离器上拆分
)。如果等于2,就意味着必须加零,否则就不能加

$d=gc .\tmp.txt
$rows=@()   
for ($i=0; $i -le $d.count -2; ++$i) {                                          
    if( $d[$i] -match "POSITION" ) { 
        $pos = $d[$i].Replace('POSITION="[', '').Replace(',',';').Replace(']";','').Replace(' ', '').trim()
        if (($pos -split ';').count -eq 2) {$pos += ";0"}
        $typ = $d[$i+1].Replace('TYPE=', '').Replace(';', '').Replace(' ', '').trim() 
        $ori = $d[$i+2].Replace('AZIMUT="', '').Replace('"', '').Replace('PARENT=;', '').Replace(' ', '').trim() 
        $rows += $("{0};{1};{2}" -f $typ,$pos,$ori)            
    } 
}
$rows