PowerShell - CSV文件中的标头并修改特定列中的值

时间:2018-05-29 10:04:27

标签: powershell csv header arithmetic-expressions

我是Powershell的新人,我希望得到一些问题的答案。

我想做的事: 我有一个没有Header的CSV文件(大约30-35列),我想修改第20列中的值。我会将此值替换为(newvalue = this value * 5)。

我的问题: - 是否有义务为我的CSV文件创建标题以在第20列访问以修改我的值? (如果是的话,怎么办?) - 我如何通过以下方式修改colum20中我的CSV文件的每一行: newvalue = column20.value * 5

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

$csvImport = Import-Csv .\listOfUsers.csv -Header "name","samname","fullname","firstName","lastname","description","company","country","city","multi","targetNum"


foreach ($item in $csvImport){
    [int]$item.targetNum= [int]$item.targetNum * 5
}
$csvImport | ConvertTo-Csv -NoTypeInformation | select -Skip 1 | Set-Content .\listOfUsers.csv

我选择了第11列,但它也适用于第20列。

答案 1 :(得分:0)

出于演示目的,使用以下脚本

  • 首先生成具有随机列数的文件noHeader.csv (10..15),行(5..10)和内容。
  • 第二部分读取第一行,将其分为','逗号来 获取列数并构建一个伪标头H1,H2,...Hx x是列数。
  • import-csv文件并将H10乘以5(转换为加倍)
## Q:\Test\2018\05\29\SO_50581599.ps1
$CsvFile = '.\NoHeader.csv'
$NewFile  = '.\WithHeader.csv'
## ======================================================================
## Generate Csv without Header with random no of cols,rows,content
$cols= get-random -min 10 -max 15
$rows= get-random -min 5  -max 10

$Data = ForEach ($Row in (1..$Rows)){
    (1..$Cols | ForEach-Object{Get-Random -min 100 -max 1000} ) -join ','
}
$Data | Out-File $CsvFile
"Generated {0} with {1} columns and {2} rows" -f $CsvFile,$cols,$rows
GC $CsvFile
"=" * 70
## ======================================================================
## evaluate number of columns in file $CsvFile
$ColCnt = ((Get-Content $CsvFile | Select -First 1) -split ',').Count
"File {0} has {1} columns" -f $CsvFile,$ColCnt

## generate Header H1..Hx
$Header = (1..$ColCnt | ForEach-Object{"H{0}" -f $_})

$NewCsv = ForEach($row in (Import-Csv $CsvFile -Header $Header)){
    [double]$row.H10 *= 5
    $row
}
$NewCsv |ft * -auto
$NewCsv | Export-csv $NewFile -NoType
Generated .\NoHeader.csv with 11 columns and 8 rows
591,864,196,281,599,216,152,236,621,311,934
942,222,522,590,649,788,421,665,493,116,282
359,984,645,276,297,320,632,934,296,197,750
746,486,375,756,462,286,977,834,765,828,907
156,899,258,941,222,517,170,301,739,501,414
395,891,182,224,430,773,115,645,114,919,401
743,475,335,650,222,899,690,442,525,695,539
861,341,114,498,550,824,635,407,235,285,631
======================================================================
File .\NoHeader.csv has 11 columns

H1  H2  H3  H4  H5  H6  H7  H8  H9   H10 H11
--  --  --  --  --  --  --  --  --   --- ---
591 864 196 281 599 216 152 236 621 1555 934
942 222 522 590 649 788 421 665 493  580 282
359 984 645 276 297 320 632 934 296  985 750
746 486 375 756 462 286 977 834 765 4140 907
156 899 258 941 222 517 170 301 739 2505 414
395 891 182 224 430 773 115 645 114 4595 401
743 475 335 650 222 899 690 442 525 3475 539
861 341 114 498 550 824 635 407 235 1425 631

答案 2 :(得分:0)

最后我按照自己的意愿得到它:

因重音字符

而使用Unicode
    $FicCSV = "C:\Temp\mycsvfile.csv"
Get-Content $FicCSV | Out-File "C:\Temp\mycsvfile_Unicode.csv" -Encoding Unicode
$csvImport = Import-Csv -path "C:\Temp\mycsvfile_Unicode.csv" -Header "A","B","C","D","E","F","G","H","I","J","K","L1","L2","M","N","O","P","Q","R1","R2","R3","R4","R5","R6","R7","R8","R9","S","T","U","V","W" -delimiter ';'

进行数学运算的小数

    foreach ($item in $csvImport){
    [decimal]$item.P= [decimal]$item.P * 5
}

我的文件必须包含以下记录:lea; john; katy; 10.2; george; ..

$csvImport | ConvertTo-Csv -NoTypeInformation -Delimiter ';' | % {$_.replace('"','').replace(',','.')}| select -Skip 1 | Set-Content "C:\Temp\mycsvfile_back.csv"

感谢帮助我成功的EveryOne:)