我有一个文本文件:
FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0017;random information;IT0077;IT0105;... FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0006;IT0017;random information;IT0077;NaN;IT0105;... FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0105;... FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0006;IT0105;...
基于3个字段:IT0006
IT0017
random data
IT0017; random information;IT0077
但没有IT0006
(column_23)和NaN
(column_27)IT0006
字段。我想创建此输出:
FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0006;IT0017;random information;IT0077;NaN;IT0105;... FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0006;IT0017;random information;IT0077;NaN;IT0105;... FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0006;IT0017;NaN;IT0077;NaN;IT0105;... FIELD1;FIELD2;FIELD3;FIELD4;FIELD5;;FIELD7;FIELD8;FIELD9;FIELD10;FIELD11;FIELD12;FIELD13;FIELD14;FIELD15;FIELD16;FIELD17;FIELD18;FIELD19;FIELD20;FIELD21;FIELD22;IT0006;IT0017;NaN;IT0077;NaN;IT0105;...
获得相同数量的列...
IT0006;
IT0006; IT0017; and NaN
(因为我没有信息并使用NaN ......)IT0017; NaN
这是使用PHP和良好工作的解决方案:
<?php
$sapFile = fopen("file.txt", "r") or die("Unable to open file!");
$typePers = array("Personale Viaggiante","Personale Interno");
$excludeFields = array("IT0006","IT0017","IT0077","Personale Viaggiante","Personale Interno","IT0105");
$emptyValue = "NaN";
$rowfinal = "";
// Output one line until end-of-file
while(!feof($sapFile)) {
$row = explode(";",fgets($sapFile));
$substring = "IT0006;IT0017;";
$lstr = "";
$rstr = "";
$type = array_intersect($row, $typePers);
$keys = array_keys($type);
if($keys == null) $type = $emptyValue;
else $type = $row[$keys[0]];
$j = 0;
for($i=0; $i<count($row); $i++){
if(in_array($row[$i],$excludeFields)){
unset($row[$i]);
$j++;
}
}
for($i=0; $i<22;$i++)$lstr .= $row[$i].";";
foreach($row as $k => $v)
if($k > 22)if(!empty($v))$rstr .= $v.";";
$rstr = substr($rstr,0,-1);
$substring .= $type;
$substring .= ";IT0077;NaN;IT0105;";
$rowfinal .= $lstr.$substring.$rstr;
}
$file = 'test.txt';
// Write the contents back to the file
file_put_contents($file, $rowfinal);
fclose($sapFile);
?>
最后,我必须将文件导出为CSV(添加标题列)
你能帮我在powershell脚本中进行转换吗?
答案 0 :(得分:3)
如果您知道只是按顺序错过了这些字段,则只需将replace运算符与正则表达式一起使用(lookbehind + negative lookahead)。
$text = Get-Content .\test.txt
$text = $text -replace '(?<=City;;\d{4};)(?!IT0006)', 'IT0006;'
$text = $text -replace '(?<=IT0006;)(?!IT0017)', 'IT0017;'
$text = $text -replace '(?<=IT0017;)(?!XXXXX)', 'NaN;'