保留数字字符使用PowerShell将非数字字符复制到相邻列CSV

时间:2019-01-24 02:13:41

标签: powershell csv scripting

我是PowerShell的新手,我一直在研究小型项目以学习如何使用它。 目前,我有一个包含.csv文件的文件夹,如下所示:

"1","2","3","4","5","6","7","8","9","10","11"  
"Calss Section:","Section1",,,,,,,,,  
"Date","01/00/00",,,,,,,,,  
"ID Number:","1234567",,,,,,,,,  
"Name:","John D",,,,,,,,,  
"RunID","type","checktype","locname","type","instrument","limit1","limit2","read data","vIR","result"
"Values",,,,,,,,,,
"1","measurement1","PCB 1_1","A1 to B1","Res","<>","0.00","5.00m","4.25m","ohm","P"

我需要对文件进行一些操作,而不是一个接一个地执行,我想编写一个脚本来帮助我遍历每个文件并进行修改。

我的目标是从“读取数据”列中拆分非数字值,因此在我的示例中应从“ 4.25m” ...变为“ 4.25”,“ m”,“ ohm”,“ P” ” 我真的只想在特定的列上做

我认为帖子中的脚本 PowerShell - Replace a character in a specific column in a csv file 是我的出发点

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

下面是我的解决方法


    $xl=New-Object -ComObject Excel.Application  
    $xl.Visible = $True  
    $wb=$xl.workbooks.open("C:\Name2.csv")  
    $ws = $wb.Worksheets(1)  
    $range = $xl.Range("I1:J1").EntireColumn  
    $range.Insert(-4161)  
    $ws.Cells(28,9) = '=IF(ISNUMBER(RIGHT(H28,1)/1),H28,LEFT(H28,LEN(H28)-1))'  
    $ws.Cells(28,10) = '=IF(ISNUMBER(FIND("m",H28)),"m",IF(ISNUMBER(FIND("M",H28)),"M",IF(ISNUMBER(FIND("k",H28)),"k",IF(ISNUMBER(FIND("n",H28)),"n",IF(ISNUMBER(FIND("u",H28)),"u","N/A")))))'  

$xl.ActiveSheet.Range("I28:I800").FillDown()