2016-11-10 66 views
0

我有一個包含五列的csv文件。最後一列是學生的年級。我想要做的是根據等級級別來更改該列的值。例如,如果gradelevel是12,我想將其更改爲2016,11至2017,等等。使用powershell更改列中的值

更新:我沒有使用它的下面去半工作:

Get-Content users.csv | ForEach-Object -Process {$_ -replace '12','2016'} | Set-Content users1.csv 

發生的事情是,如果學生證中有一個12得到改變,以及至2016年的例子是120045得到改變到20160045

+0

你到目前爲止嘗試過什麼?另外,請分享CSV **和**預期結果的示例。 –

回答

0

可以導入CSV,循環它在foreach和使用$ _(本)運營商,然後出口IST到csv

Somesthing像:

# import CSV 
$list = import-csv P:\ath\to\file.csv -Delimiter ";" -Encoding Standard 
# % means foreach 
$list | % { 
    # The grades is the key for each line 
    # the first line of your csv represent the keys you can use 
    # e.g. first colum: Name | Grade | Class 
    # $_.Name | $_.Grade | $_.Class are your keys for every entry (2nd line and above) 

    # here you can work with your grades an do what you want 
    if($_.Grade -eq 11){ 
    # set a new value to the grade in the actual line 
    $_.Grade = 2016 
    } 
} 

# now export the new list into csv 
export-csv P:\ath\to\new.csv -Delimiter ";" -NoTypeInformation 

這應該是一個基本的工作。

Greetz Eldo.O

+0

我在命令管道位置獲取cmdlet Export-Csv 1 以下參數的供應值: InputObject: –

0

Eldo you code worked great。我也得最後一行從改變:

export-csv P:\ath\to\new.csv -Delimiter ";" -NoTypeInformation 

$list | Export-Csv P:\ath\to\new.csv -Delimiter "," -NoTypeInformation 

我也可以添加更多的if語句來完成正是我需要的。

+0

哦 - 我忘了:D - 我沒有檢查runspace中的代碼,直接將其編碼到anwaser中:P但很高興看到,它是爲你工作。 –